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.

3
  • @Kayaman Sorry for the confusion. it's a typo. Corrected it in json. Commented Jun 3, 2021 at 21:07
  • 1
    JSON payload fits given Java POJOs classes. There must be a problem somewhere else. Commented Jun 4, 2021 at 12:19
  • 1
    It was my mistake. Upon rechecking entire JSON, had one field with wrong name, somehow missed as it was big json. Thanks anyway for all. Commented Jun 5, 2021 at 23:54

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.