0

I am trying to de-serialize the json string to objects, and facing an issue where I have array of objects in sub level. I looked into the below stackoverflow links where they have designed one class and classes with in that for each sub level; and used List<> for array of data. I have used the same but have seprate classes for each object (separate java files)

Json string parsing to java object with multiple objects

How to use Jackson to deserialise an array of objects

Let me give you a vision of my code:

JSON for Person details

{
"name" : "abcd",
"age" : 30,
"addressList" : {
   "count" : 2,
   "data" : [
     {
      "state" : "NY",
      "city" : "Bronx"
     },
     {
      "state" : "NJ",
      "city" : "Chestnut"
     }
   ]
 }
}

Classes

public class Person {
    private String name;
    private int age;
    private AddressList addressList;

    public Person() { }

    //getters and setters
}

public class AddressList {
    private int count;
    private List<Address> data;

    public AddressList() { }

    //getters and setters
}

public class Address {
    private String state, city;

    public Address() { }

    //getters and setters
}

In my main class, doing like below:

ObjectMapper mapper = new ObjectMapper();
Defect defect = mapper.readValue(jsonString, Person.class);

Now, All the values are loading properly except the address list. Even I tried Address[] instead of List<Address> in AddressList.java but no luck. Can anyone give me some ideas on this or what could be the mistake. Thanks in advance.

Shan C
  • 87
  • 1
  • 2
  • 11

1 Answers1

0

Basically the structure above should work fine as pointed out in comments area. Initially didn't work for me because I have missed to notice on of my field name was wrong, by correcting that working like charm.

Shan C
  • 87
  • 1
  • 2
  • 11