1

I have a Json list using object with children

{
 "id":"154",
 "name":"peter",
 "children": [
               {
                "id":"122",
                "name": "mick",
                "children":[]
               },
               {
                "id":"123",
                "name": "mick",
                "children":[]
               }
            ]
}

Here is the class of my object:

public class person{
                    private String id;
                    private String name;
                    private List<person> children;

//getters and setters
}

When I try to deserialize this object, I have the following error

Can not deserialize instance of person out of START_ARRAY token

What should I do ?

Jean
  • 601
  • 1
  • 10
  • 26

4 Answers4

1

The JSON contains an array of persons. Your class a List of person.

Either change the JSON like @Naveed Yadav suggested or change the class to

public class Person{
             private String id;
             private String name;
             private Person[] children;

//getters and setters
}

(BTW the class name should be upper case in Java)

Datz
  • 3,156
  • 3
  • 22
  • 50
1

Fix syntax errors in your JSON body and you'll be in a good shape:

{
 "id":"154",
 "name":"peter",
 "children": [
               {
                "id":"122",
                "name": "mick",
                "children":[], <== Excess comma
               } <== Missing comma 
               {
                "id":"123",
                "name": "mick",
                "children":[], <== Excess comma
               }
            ]
}

Valid one:

{
    "id": "154",
    "name": "peter",
    "children": [{
            "id": "122",
            "name": "mick",
            "children": []
        },
        {
            "id": "123",
            "name": "mick",
            "children": []
        }
    ]
}
J-Alex
  • 6,881
  • 10
  • 46
  • 64
0

You need to change your POJO declaration like below:-

 public class person{
                private String id;
                private String name;
                private List<Children> children;

               //getters and setters

   private class Children{
                private String id;
                private String name;
                private String[] children;

}
Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17
-1
{
 "id":"154",
 "name":"peter",
 "children": 
               {
                "id":"122",
                "name": "mick",
                "children":[],
               }
               {
                "id":"123",
                "name": "mick",
                "children":[],
               }

}
Naveen Yadav
  • 203
  • 9
  • 25