简体   繁体   English

Jackson: Deserialize JSON array from JSON object into Java List

[英]Jackson: Deserialize JSON array from JSON object into Java List

I have been stumbled by this for a while.我已经被这个绊倒了一段时间。 I have a Spring application and would like to parse the following JSON:我有一个 Spring 应用程序并想解析以下 JSON:

{
  "metadata": {...}
  "response": {
    "objects": [
      {
        "name": "someName",
        "properties": [<array_of_properties>]
      },
      ...
    ]
  }
}

into a list of the following Java objects:进入以下 Java 对象的列表:

public class MyClass {
  String name;
  List<CustomProperties> customProperties;
}

Meaning, I want to extract only the objects array and parse only that.意思是,我只想提取objects数组并仅解析它。 I have tried using a custom deserializer and that works, but I had to do:我曾尝试使用自定义反序列化器并且有效,但我必须这样做:

@JsonDeserialize(using=MyDeserializer.class)
public class MyClassList extends ArrayList<MyClass>{}

and then:接着:

ObjectMapper objectMapper = new ObjectMapper();
List<MyClass> list = objectMapper.readValue(json, MyClassList.class) 

Is there anyway to avoid extending ArrayList , since currently I am doing that in order to be able to access the .class property.无论如何要避免扩展ArrayList ,因为目前我这样做是为了能够访问.class属性。

you can define your json structure with a couple of classes你可以用几个类定义你的 json 结构

public class MyJson {
  private MyResponse response;
  ...
}

public class MyResponse {
  private List<MyClass> objects;
  ...
}

public class MyClass {
  String name;
  List<CustomProperty> customProperties;
  ...
}

than you can use Jackson to parse the json string to MyJson class, no special @JsonDeserialize is needed您可以使用 Jackson 将 json 字符串解析为MyJson class,不需要特殊的@JsonDeserialize

ObjectMapper objectMapper = new ObjectMapper();
MyJson myJson = objectMapper.readValue(json, MyJson.class);
List<MyClass> list =  myJson.getResponse().getObjects();

Keep in mind, this code is only a draft, all classes should have setters (and getters) and some null checks are required请记住,此代码只是草稿,所有类都应该有设置器(和获取器),并且需要一些 null 检查

You can do something like this.你可以做这样的事情。 I feel this would be cleaner我觉得这会更干净

@JsonIgnoreProperties(ignoreUnknown = true)
class Wrapper{
  private Response response;
  //setters, getters
}

@JsonIgnoreProperties(ignoreUnknown = true)
class Response{
  private List<MyClass> objects;
  //setters, getters
}

@JsonIgnoreProperties(ignoreUnknown = true)
public class MyClass {
  String name;
  List<CustomProperties> customProperties;
  //setters, getters
}

ObjectMapper objectMapper = new ObjectMapper();
Wrapper wrapper = objectMapper.readValue(json, Wrapper.class) 

You can extrat objects and consequently CustomProperties by traversing the list.您可以通过遍历列表来提取对象和 CustomProperties。 You can declare only fields which you are interested in and ignore others by @JsonIgnoreProperties(ignoreUnknown = true) (for example i have not included metadata)您可以通过@JsonIgnoreProperties(ignoreUnknown = true)仅声明您感兴趣的字段并忽略其他字段(例如,我没有包含元数据)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM