简体   繁体   English

Spring Jackson将json对象转换为Java数组

[英]Spring Jackson convert json object to java array

I have a json like below : 我有一个像下面的json:

{"key":{"a":"aValue"}} {“ key”:{“ a”:“ aValue”}}

"key" can contain json object as well as json array. “键”可以包含json对象以及json数组。 i have created following java object to map this json : 我创建了以下java对象来映射此json:

Class Output {
  private List<DummyObject> key;
  // setter, getting ommited
}
Class DummyObject {
  private String a;
}

So, i want if json is 所以,我想如果json是

{"key":[{"a":"val1"},{"a":"val2"}]} {“ key”:[{“ a”:“ val1”},{“ a”:“ val2”}]}

"key" property of Output class should contain a list of 2 objects, and when the json is Output类的“ key”属性应包含2个对象的列表,并且当json为

{"key":{"a":"val1"}} {“ key”:{“ a”:“ val1”}}

"key" should contain a list of 1 object only. “键”应仅包含1个对象的列表。

I have tried using deserializer but it does not work. 我尝试使用反序列化器,但是它不起作用。 Also, i do not want to deserialise DummyObject myself. 另外,我也不想自己反序列化DummyObject

Try enabling the Jackson deserialization feature ACCEPT_SINGLE_VALUE_AS_ARRAY . 尝试启用Jackson的反序列化功能ACCEPT_SINGLE_VALUE_AS_ARRAY

final ObjectMapper mapper = new ObjectMapper()
        .enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);

Considering {"key":[{"a":"val1"},{"a":"val2"}]} 考虑到{"key":[{"a":"val1"},{"a":"val2"}]}

The issue is not in your model with the Output; 问题不在输出模型中;

Class Output {
  private List<DummyObject> key;
  // setter, getting ommited
}

Since this represents that key as a json array. 由于此key将该key表示为json数组。

But you might want to update the DummyObject: 但是您可能想要更新DummyObject:

Class DummyObject {
  private String a;
}

to

Class DummyObject {
  private Map<String,String> a;
}

Since {"a":"val1"} is not a valid representation of DummyObject or even String a . 由于{“ a”:“ val1”}不是DummyObject甚至String a的有效表示。


Additionally as pointed out by @tima, for varying length of your JSONArray for "key", you must take a look at how to Make Jackson interpret single JSON object as array with one element 另外,如@tima所指出的,对于“键”的JSONArray长度不同,您必须了解如何使Jackson将单个JSON对象解释为具有一个元素的数组


The final clause of your question: 您问题的最后一句:

Also, I do not want to deserialize DummyObject myself. 另外,我也不想自己反序列化DummyObject。

You can try and update the Output model(not sure if that would be beneficial from performance perspective) as: 您可以尝试更新输出模型(从性能角度来看不确定是否会有所帮助),如下所示:

Class Output {
  private List<Map<String, String>> key;
  // setter, getting ommited
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用杰克逊将json数组转换为数组中具有不同对象元素的java对象 - Convert json array to java object with different object element in array using jackson 使用Jackson和Spring MVC 3.1将Java列表转换为以列表对象的属性为键的JSON对象 - Convert Java List to a JSON Object keyed by attribute of the list objects using Jackson and Spring MVC 3.1 使用Jackson将json数组反序列化为Java对象 - deserialize json array to java object using Jackson 使用Jackson将JSON数组反序列化为单个Java对象 - Deserialize JSON array to a single Java object with Jackson 杰克逊将JSON转换为对象 - convert JSON to Object by Jackson 通过Jackson将JSON转换为JAVA - Convert JSON to JAVA by jackson Java/jackson 对象映射:如何将嵌套的 JSON 数组转换为对象列表(最好用一个例子来解释) - Java/jackson object mapping: how to convert a nested JSON array to a list of objects (might be best explain with an example) 如何在Spring Boot上将具有数组元素的JSON转换为Java对象 - How to convert a JSON with array elements to a Java Object on Spring Boot 如何转换ArrayList <String> 在Spring @ResponseBody中使用Jackson的JSON对象 - How to convert ArrayList<String> to JSON object using Jackson in Spring @ResponseBody 如何使用Jackson JSON将K / V的json数组转换为Java HashMap - How to convert json array of K/V to Java HashMap with Jackson JSON
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM