简体   繁体   English

用杰克逊反序列化复杂的Json

[英]Deserialize complex Json with Jackson

I ran into an issue with a "complex" json : 我遇到了“复杂” json的问题:

{
  "Books": [
    {
      "title": "Java",
      "instructions": [],
      "links": {
        "EJB": {
          "href": "www.java.com/EJB"
        }
      }
    },
    {
      "title": "C#",
      "instructions": [],
      "links": {
        "SOAP": {
          "href": "www.C#.com/SOAP"
        }
      }
    }]
}

Can someone tell me how to deserialize this kind of JSON to get a list of POJO please ? 有人可以告诉我如何反序列化这种JSON以获得POJO列表吗?

The POJO has to be a "Book" with 3 fields : title , instructions and the links POJO必须是一本包含3个字段的“书”:标题,说明和链接

thank you. 谢谢。

You have a fundamental problem with your JSON for starters; 对于初学者来说,您的JSON有一个基本问题; you have duplicate member names. 您有重复的会员名。

And in this case, the behaviour of the parser is undefined ( RFC 7159, section 4 ). 在这种情况下,解析器的行为是不确定的RFC 7159,第4节 )。 Since jsonschema2pojo uses Jackson, this means you will get the last defined value for each duplicate member. 由于jsonschema2pojo使用Jackson,因此这意味着您将为每个重复成员获得最后定义的值。

Use an array: 使用数组:

[
{
  "title":"Title 1",
  "instructions" : [],
  "links": {
    "link 1": {
      "href": "The link 1"
    }
  }
},
{
   "title":"Title 2",
   "instructions" : [],
   "links": {
    "link 2": {
      "href": "The link 2"
    }
  }
}
]

Then, jsonschema2pojo will not really deserialize to a POJO; 然后,jsonschema2pojo不会真正反序列化为POJO。 what you really want is to define class MyClass for an array element (ie, one object) and use Jackson's ObjectMapper to deserialize: 您真正想要的是为数组元素(即,一个对象)定义类MyClass ,然后使用Jackson的ObjectMapper反序列化:

final TypeReference<List<MyClass>> typeref
    = new TypeReference<List<MyClass>>() {};

final ObjectMapper mapper = new ObjectMapper();

final List<MyClass> list = mapper.readValue(whatever, typeref);

Your POJO classes could look like this: 您的POJO类可能如下所示:

class Books {

    @JsonProperty("Books")
    private List<Book> books;

    public List<Book> getBooks() {
        return books;
    }

    public void setBooks(List<Book> books) {
        this.books = books;
    }

    @Override
    public String toString() {
        return "Books [books=" + books + "]";
    }
}

class Book {

    private String title;
    private List<String> instructions;
    private Map<String, Link> links;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public List<String> getInstructions() {
        return instructions;
    }

    public void setInstructions(List<String> instructions) {
        this.instructions = instructions;
    }

    public Map<String, Link> getLinks() {
        return links;
    }

    public void setLinks(Map<String, Link> links) {
        this.links = links;
    }

    @Override
    public String toString() {
        return "Book [title=" + title + ", instructions=" + instructions + ", links=" + links + "]";
    }
}

class Link {

    private String href;

    public String getHref() {
        return href;
    }

    public void setHref(String href) {
        this.href = href;
    }

    @Override
    public String toString() {
        return href;
    }
}

Simple usage: 简单用法:

ObjectMapper mapper = new ObjectMapper();

System.out.println(mapper.readValue(json, Books.class));

Above program prints: 上面的程序打印:

Books [books=[Book [title=Java, instructions=[], links={EJB=www.java.com/EJB}], Book [title=C#, instructions=[], links={SOAP=www.C#.com/SOAP}]]]

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

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