-
月关宝盒
我的服务可以接收几种不同的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 { public static void main(String[] args) { 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}"; String jsonB = "{\"event\":\"message\",\"message\":{\"type\":\"text\",\"text\":\"a message to the service\",\"location\":{\"lat\":12.34,\"lon\":12.34}}}"; JSONObject jsonObject = JSONObject.parseObject(jsonA); String event = jsonObject.getString("event"); if (event.equals("message")) { //do what you want to do System.out.println("message event......"); } else if ("conversation_started".equals(event)) { System.out.println("context information event......"); } }}如下声明一个类Event,然后将 JSON String 转换为EventJAVA 对象。@Datapublic class Event { private String event; private String context; private User user; private boolean subscribed; private Message message; @Data public static class User { private String id; private String name; private String avatar; private String country; private String language; private int api_version; } @Data public static class Message { private String type; private String text; private Location location; @Data public static class Location { private double lat; private double lon; } } public static void main(String[] args) { 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}"; String jsonB = "{\"event\":\"message\",\"message\":{\"type\":\"text\",\"text\":\"a message to the service\",\"location\":{\"lat\":12.34,\"lon\":12.34}}}"; ObjectMapper objectMapper = new ObjectMapper(); try { Event eventA = objectMapper.readValue(jsonA, new TypeReference<Event>() { }); System.out.println(objectMapper.writeValueAsString(eventA)); Event eventB = objectMapper.readValue(jsonB, new TypeReference<Event>() { }); System.out.println(objectMapper.writeValueAsString(eventB)); } catch (IOException e) { e.printStackTrace(); } }}
-
FFIVE
使用 JSON 对象。这是动态的,可以加载任何 json。然后你就可以一致地引用事件字段实施例1 //import java.util.ArrayList; //import org.bson.Document; 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 }"); System.out.println(((String)root.get("event")));实施例2 //import java.util.ArrayList; //import org.bson.Document; Document root = Document.parse("{ \"event\" : \"message\", \"message\" : { \"type\" : \"text\", \"text\" : \"a message to the service\", \"location\" : { \"lat\" : 12.34, \"lon\" : 12.34 } } }"); System.out.println(((String)root.get("event")));