Java - How to deserialize a JSON array with nested objects and lists?

14,411

As I indicated in the comment to the question here is the test that shows how to do this:

public class JSONTest {

  public static class FullyTypedThing {
    public int id;
    public String name;
    public List<Photos> photos = Lists.newArrayList();
    public String username;

    public static class Photos {
      public String name;
      public String url;
    }
  }

  private static final String json = "[ { \"id\" : 33147,    \"name\" : \"Refinancing\", \"photos\" : [ {"
    + "\"name\" : \"347.png\", \"url\" : \"/loans/568/photos/092\"  } ],"
    + "\"username\" : \"zach1985\"} , {  \"id\" : 7693,  \"name\" : \"Stuff\","
    + "\"photos\" : [ {  \"name\" : \"newpic1.png\",  \"url\" : \"/loans/123446/photos/890\"  } ],"
    + "\"username\" : \"sengaia\"  } ]";

  @Test
  public void roundtrip() throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    List<FullyTypedThing> res = Lists.newArrayList(
      mapper.readValue(json, FullyTypedThing[].class));
    assertEquals(2, res.size());
    assertEquals(33147, res.get(0).id);
    assertEquals("Refinancing", res.get(0).name);
    assertEquals("347.png", res.get(0).photos.get(0).name);
    assertEquals("/loans/568/photos/092", res.get(0).photos.get(0).url);
    assertEquals(7693, res.get(1).id);
    assertEquals("Stuff", res.get(1).name);
    assertEquals("newpic1.png", res.get(1).photos.get(0).name);
    assertEquals("/loans/123446/photos/890", res.get(1).photos.get(0).url);

    assertEquals("[{\"id\":33147,\"name\":\"Refinancing\",\"photos\":"
      + "[{\"name\":\"347.png\",\"url\":\"/loans/568/photos/092\"}],"
      + "\"username\":\"zach1985\"},{\"id\":7693,\"name\":\"Stuff\","
      + "\"photos\":[{\"name\":\"newpic1.png\",\"url\":\"/loans/123446/photos/890\"}],"
      + "\"username\":\"sengaia\"}]", mapper.writeValueAsString(res));

  }
}
Share:
14,411
Sargon1
Author by

Sargon1

On my way to become a paid programmer.

Updated on June 05, 2022

Comments

  • Sargon1
    Sargon1 almost 2 years

    How to deserialize a JSON String that contains lists of objects within other objects? I found explanations for simple deserialization, but I can't extrapolate much from them, as they're all a little bit off. As an example(POJOs ommited), for

    String json = "[ {
      "id" : 33147,
      "name" : "Refinancing",
      "photos" : [ {
        "name" : "347.png",
        "url" : "/loans/568/photos/092"
      } ],
      "username" : "zach1985"
    } , {
      "id" : 7693,
      "name" : "Stuff",
      "photos" : [ {
        "name" : "newpic1.png",
        "url" : "/loans/123446/photos/890"
      } ],
      "username" : "sengaia"
    } ]";
    
    ArrayList<Ad> ads = new ArrayList<>;
    
    deserialize(json, ads);
    
    System.out.println(ads.get(1).getName());
    System.out.println(ads.get(0).getPhotos().get(0).getName());
    

    The outputs would be "Stuff" and "347". How would then the deserialize() method need to be implemented?