除了 Jackson 之外,还有其他东西可以用 Java 处理复杂的对象吗?

我必须能够在后端接收/发送一个JSON结构,如下面的那个。


{  

   "firstObject":{  

      "type":"string",

      "value":"productValue"

   },

   "secondObject":{  

      "type":"string",

      "value":"statusValue"

   },

   "thirdObject":{  

      "type":"map",

      "value":{  

         "customerName":{  

            "type":"string",

            "value":"customerValue"

         }

      }

   },

   "fourthObject":{  

      "type":"map",

      "value":{  

         "firstObject":{  

            "type":"map",

            "value":{  

               "anotherObj1":{  

                  "type":"string",

                  "value":"TEST"

               },

               "anotherObj2":{  

                  "type":"date",

                  "value":"01/12/2018"

               },

               "anotherObj3":{  

                  "type":"date",

                  "value":"31/01/2018"

               }

            }

         }

      }

   }

}

让这有点棘手的问题是,对于每个对象,我必须知道它的类型是什么。可能有4种类型:

  • 整数

  • 细绳

  • 布尔值

  • 地图

如果一个对象的值是map(由客户选择的),例如,在前端会出现另一个键/值结构,所以我在后端会收到一个动态结构。我需要对此结构进行验证,以检查它是否符合我期望收到的内容。

如果我应该只使用一个Java类来制作我需要的对象,或者除此之外使用JacksonJSON验证并将所有这些对象映射到JSON.

如果我要使用 Jackson,我将不得不制作一个自定义的序列化器和反序列化器。


炎炎设计
浏览 89回答 2
2回答

眼眸繁星

您可以从Jackson库中使用JsonTypeInfo和JsonSubTypes注释。它们是句柄多态类型处理:@JsonTypeInfo用于指示序列化中包含哪些类型信息的详细信息@JsonSubTypes用于表示注解类型的子类型@JsonTypeName用于定义用于注释类的逻辑类型名称您的示例适合此解决方案,除了看起来更像简单POJO类的根对象。在您的情况下,我们应该创建有助于使用以下 3 种类型的类型结构:string,&nbsp;date,&nbsp;map:@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")@JsonSubTypes({&nbsp; &nbsp; @JsonSubTypes.Type(value = StringValue.class, name = "string"),&nbsp; &nbsp; @JsonSubTypes.Type(value = DateValue.class, name = "date"),&nbsp; &nbsp; @JsonSubTypes.Type(value = MapValue.class, name = "map")})abstract class HasValue<T> {&nbsp; &nbsp; protected T value;&nbsp; &nbsp; public HasValue() {&nbsp; &nbsp; &nbsp; &nbsp; this(null);&nbsp; &nbsp; }&nbsp; &nbsp; public HasValue(T value) {&nbsp; &nbsp; &nbsp; &nbsp; this.value = value;&nbsp; &nbsp; }&nbsp; &nbsp; public T getValue() {&nbsp; &nbsp; &nbsp; &nbsp; return value;&nbsp; &nbsp; }&nbsp; &nbsp; public void setValue(T value) {&nbsp; &nbsp; &nbsp; &nbsp; this.value = value;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; return getClass().getSimpleName() + "{" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "value=" + value +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "}";&nbsp; &nbsp; }}class StringValue extends HasValue<String> {&nbsp; &nbsp; public StringValue() {&nbsp; &nbsp; &nbsp; &nbsp; this(null);&nbsp; &nbsp; }&nbsp; &nbsp; public StringValue(String value) {&nbsp; &nbsp; &nbsp; &nbsp; super(value);&nbsp; &nbsp; }}class DateValue extends HasValue<String> {&nbsp; &nbsp; public DateValue(String value) {&nbsp; &nbsp; &nbsp; &nbsp; super(value);&nbsp; &nbsp; }&nbsp; &nbsp; public DateValue() {&nbsp; &nbsp; &nbsp; &nbsp; this(null);&nbsp; &nbsp; }}class MapValue extends HasValue<Map<String, HasValue>> {&nbsp; &nbsp; public MapValue(Map<String, HasValue> value) {&nbsp; &nbsp; &nbsp; &nbsp; super(value);&nbsp; &nbsp; }&nbsp; &nbsp; public MapValue() {&nbsp; &nbsp; &nbsp; &nbsp; this(new LinkedHashMap<>());&nbsp; &nbsp; }&nbsp; &nbsp; public void put(String key, HasValue hasValue) {&nbsp; &nbsp; &nbsp; &nbsp; this.value.put(key, hasValue);&nbsp; &nbsp; }}现在,我们需要引入POJO根值。它可能如下所示,但您可以根据需要添加 getter/setter。对于这个例子,下面的代码就足够了:class Root {&nbsp; &nbsp; public HasValue firstObject;&nbsp; &nbsp; public HasValue secondObject;&nbsp; &nbsp; public HasValue thirdObject;&nbsp; &nbsp; public HasValue fourthObject;&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; return "Root{" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "firstObject=" + firstObject +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ", secondObject=" + secondObject +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ", thirdObject=" + thirdObject +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ", fourthObject=" + fourthObject +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '}';&nbsp; &nbsp; }}现在,我们终于可以尝试序列化和反序列化这些对象了:MapValue customerName = new MapValue();customerName.put("customerName", new StringValue("customerValue"));MapValue innerMap = new MapValue();innerMap.put("anotherObj1", new StringValue("TEST"));innerMap.put("anotherObj2", new DateValue("01/12/2018"));innerMap.put("anotherObj3", new DateValue("31/01/2018"));MapValue fourthObject = new MapValue();fourthObject.put("firstObject", innerMap);Root root = new Root();root.firstObject = new StringValue("productValue");root.secondObject = new StringValue("statusValue");root.thirdObject = customerName;root.fourthObject = fourthObject;ObjectMapper mapper = new ObjectMapper();String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(root);System.out.println(json);System.out.println(mapper.readValue(json, Root.class));Aboce 代码打印JSON:{&nbsp; "firstObject" : {&nbsp; &nbsp; "type" : "string",&nbsp; &nbsp; "value" : "productValue"&nbsp; },&nbsp; "secondObject" : {&nbsp; &nbsp; "type" : "string",&nbsp; &nbsp; "value" : "statusValue"&nbsp; },&nbsp; "thirdObject" : {&nbsp; &nbsp; "type" : "map",&nbsp; &nbsp; "value" : {&nbsp; &nbsp; &nbsp; "customerName" : {&nbsp; &nbsp; &nbsp; &nbsp; "type" : "string",&nbsp; &nbsp; &nbsp; &nbsp; "value" : "customerValue"&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; },&nbsp; "fourthObject" : {&nbsp; &nbsp; "type" : "map",&nbsp; &nbsp; "value" : {&nbsp; &nbsp; &nbsp; "firstObject" : {&nbsp; &nbsp; &nbsp; &nbsp; "type" : "map",&nbsp; &nbsp; &nbsp; &nbsp; "value" : {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "anotherObj1" : {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "type" : "string",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "value" : "TEST"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "anotherObj2" : {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "type" : "date",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "value" : "01/12/2018"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "anotherObj3" : {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "type" : "date",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "value" : "31/01/2018"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; }}和toString表示:Root{firstObject=StringValue{value=productValue}, secondObject=StringValue{value=statusValue}, thirdObject=MapValue{value={customerName=StringValue{value=customerValue}}}, fourthObject=MapValue{value={firstObject=MapValue{value={anotherObj1=StringValue{value=TEST}, anotherObj2=DateValue{value=01/12/2018}, anotherObj3=DateValue{value=31/01/2018}}}}}}您可以通过添加/删除任何类型的HasValue实例轻松地操作输出。

MMMHUHU

从这里下载 JSON jar 。从客户端使用 json.stringify 将您的 JSON 转换为字符串(因为 JSONObject 的构造函数只接受字符串)。收到客户端的请求后,执行以下操作:public void doPost(request,response) throws ParseException, JSONException {&nbsp; &nbsp; &nbsp; &nbsp; parseMapFromJSON(request.getParameter("JSONFromClient"));}private void parseMapFromJSON(String JSONParam) throws JSONException{&nbsp; &nbsp; JSONObject requestJSON = new JSONObject(JSONParam);&nbsp; &nbsp; for(int i=0; i<requestJSON.length();i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;String key = (String) requestJSON.names().get(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(key.endsWith("Object"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parseMapFromJSON(requestJSON.get(key).toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else if(key.startsWith("type") && (requestJSON.get(key).equals("date") || requestJSON.get(key).equals("string")))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(requestJSON.get("value"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else if(key.startsWith("type") && requestJSON.get(key).equals("map"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parseMapFromJSON(requestJSON.get("value").toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else if(!key.equals("value"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parseMapFromJSON(requestJSON.get(key).toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java