简体   繁体   中英

Deserialize complex Json with Jackson

I ran into an issue with a "complex" 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 ?

The POJO has to be a "Book" with 3 fields : title , instructions and the links

thank you.

You have a fundamental problem with your JSON for starters; you have duplicate member names.

And in this case, the behaviour of the parser is undefined ( RFC 7159, section 4 ). Since jsonschema2pojo uses Jackson, this means you will get the last defined value for each duplicate member.

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; what you really want is to define class MyClass for an array element (ie, one object) and use Jackson's ObjectMapper to deserialize:

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:

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}]]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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