如果我在解析之前不知道所有 json 字段,如何将 JSON 转换为 Java 对象?

我的服务可以接收几种不同的json,例如:


{

   "event":"conversation_started",

   "context":"context information",

   "user":{

      "id":"01234567890A=",

      "name":"John McClane",

      "avatar":"http://avatar.example.com",

      "country":"UK",

      "language":"en",

      "api_version":1

   },

   "subscribed":false

}

或者


 {

   "event":"message",

   "message":{

      "type":"text",

      "text":"a message to the service",

      "location":{

         "lat":12.34,

         "lon":12.34

      }

   }

}

或其他几个 json。所有 json 中唯一相同的字段是“event”。所有其他字段都可以不同(取决于“事件”值)。


所以问题是:如何将这些 json 转换为 java 对象(而不制作混乱的代码)?我知道的唯一方法是手动检查“事件”值(例如,json.startsWith("{\n\"event\":\"message\"")但我确信这样做有任何简单的决定。


有只小跳蛙
浏览 86回答 3
3回答

月关宝盒

我的服务可以接收几种不同的json,例如:{   "event":"conversation_started",   "context":"context information",   "user":{      "id":"01234567890A=",      "name":"John McClane",      "avatar":"http://avatar.example.com",      "country":"UK",      "language":"en",      "api_version":1   },   "subscribed":false}或者 {   "event":"message",   "message":{      "type":"text",      "text":"a message to the service",      "location":{         "lat":12.34,         "lon":12.34      }   }}或其他几个 json。所有 json 中唯一相同的字段是“event”。所有其他字段都可以不同(取决于“事件”值)。所以问题是:如何将这些 json 转换为 java 对象(而不制作混乱的代码)?我知道的唯一方法是手动检查“事件”值(例如,json.startsWith("{\n\"event\":\"message\"")但我确信这样做有任何简单的决定。

小怪兽爱吃肉

UPD:如果您不想通过声明 POJO 将 JSON 字符串转换为 JAVA 对象,您可以将其解析为JSONObject(com.alibaba.fastjson.JSONObject)public class Event {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; String jsonA = "{\"event\":\"conversation_started\",\"context\":\"context information\",\"user\":{\"id\":\"01234567890A=\",\"name\":\"John McClane\",\"avatar\":\"http://avatar.example.com\",\"country\":\"UK\",\"language\":\"en\",\"api_version\":1},\"subscribed\":false}";&nbsp; &nbsp; &nbsp; &nbsp; String jsonB = "{\"event\":\"message\",\"message\":{\"type\":\"text\",\"text\":\"a message to the service\",\"location\":{\"lat\":12.34,\"lon\":12.34}}}";&nbsp; &nbsp; &nbsp; &nbsp; JSONObject jsonObject = JSONObject.parseObject(jsonA);&nbsp; &nbsp; &nbsp; &nbsp; String event = jsonObject.getString("event");&nbsp; &nbsp; &nbsp; &nbsp; if (event.equals("message")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //do what you want to do&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("message event......");&nbsp; &nbsp; &nbsp; &nbsp; } else if ("conversation_started".equals(event)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("context information event......");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}如下声明一个类Event,然后将 JSON String 转换为EventJAVA 对象。@Datapublic class Event {&nbsp; &nbsp; private String event;&nbsp; &nbsp; private String context;&nbsp; &nbsp; private User user;&nbsp; &nbsp; private boolean subscribed;&nbsp; &nbsp; private Message message;&nbsp; &nbsp; @Data&nbsp; &nbsp; public static class User {&nbsp; &nbsp; &nbsp; &nbsp; private String id;&nbsp; &nbsp; &nbsp; &nbsp; private String name;&nbsp; &nbsp; &nbsp; &nbsp; private String avatar;&nbsp; &nbsp; &nbsp; &nbsp; private String country;&nbsp; &nbsp; &nbsp; &nbsp; private String language;&nbsp; &nbsp; &nbsp; &nbsp; private int api_version;&nbsp; &nbsp; }&nbsp; &nbsp; @Data&nbsp; &nbsp; public static class Message {&nbsp; &nbsp; &nbsp; &nbsp; private String type;&nbsp; &nbsp; &nbsp; &nbsp; private String text;&nbsp; &nbsp; &nbsp; &nbsp; private Location location;&nbsp; &nbsp; &nbsp; &nbsp; @Data&nbsp; &nbsp; &nbsp; &nbsp; public static class Location {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; private double lat;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; private double lon;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; String jsonA = "{\"event\":\"conversation_started\",\"context\":\"context information\",\"user\":{\"id\":\"01234567890A=\",\"name\":\"John McClane\",\"avatar\":\"http://avatar.example.com\",\"country\":\"UK\",\"language\":\"en\",\"api_version\":1},\"subscribed\":false}";&nbsp; &nbsp; &nbsp; &nbsp; String jsonB = "{\"event\":\"message\",\"message\":{\"type\":\"text\",\"text\":\"a message to the service\",\"location\":{\"lat\":12.34,\"lon\":12.34}}}";&nbsp; &nbsp; &nbsp; &nbsp; ObjectMapper objectMapper = new ObjectMapper();&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Event eventA = objectMapper.readValue(jsonA, new TypeReference<Event>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(objectMapper.writeValueAsString(eventA));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Event eventB = objectMapper.readValue(jsonB, new TypeReference<Event>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(objectMapper.writeValueAsString(eventB));&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

FFIVE

使用 JSON 对象。这是动态的,可以加载任何 json。然后你就可以一致地引用事件字段实施例1&nbsp;//import java.util.ArrayList;&nbsp;//import org.bson.Document;&nbsp;Document root = Document.parse("{ \"event\" : \"conversation_started\", \"context\" : \"context information\", \"user\" : { \"id\" : \"01234567890A=\", \"name\" : \"John McClane\", \"avatar\" : \"http://avatar.example.com\", \"country\" : \"UK\", \"language\" : \"en\", \"api_version\" : 1 }, \"subscribed\" : false }");&nbsp;System.out.println(((String)root.get("event")));实施例2&nbsp;//import java.util.ArrayList;&nbsp;//import org.bson.Document;&nbsp;Document root = Document.parse("{ \"event\" : \"message\", \"message\" : { \"type\" : \"text\", \"text\" : \"a message to the service\", \"location\" : { \"lat\" : 12.34, \"lon\" : 12.34 } } }");&nbsp;System.out.println(((String)root.get("event")));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java