Jean
Jean

Reputation: 621

Jackson JSon : Can not deserialize instance of java.lang.String out of START_OBJECT token

I'm using an API and I'm trying to deserialize the results with Jackson but I always have the same mistake.

I guess my POJO is not right but I can't find what's wrong.

The mistake is on libs

I've tried to declare as String or as List without any success. Any idea ?

{
"libs": {},
"items": [
    {
        "id": "001",
        "cars": [
            "cd1042af-856d-4649-a170-032d15a4119b",
            "00ed61a4-3aab-4722-90c2-7f4cca4cbded",
            "8fa3aa8b-3f22-4413-a41d-e78de9051de2"
        ],
        "name": "James"


    },
    {

        "id": "002",
        "cars": [
            "cd1043af-856d-4649-a170-032d15a4119b",
            "00ed61a4-3aab-4722-90c2-7f4cca4cbded",
            "8fa3aa8b-3f22-4413-a41d-e78de9051de2"
        ],
        "name": "James"

    }]

}

public class Page<Car> {


    private List<String> libs;

    private List<Car>items;



    public List<Car> getItems() {
        return items;
    }

    public void setItems(List<Car> items) {
        this.items = items;
    }

    public List<String> getLibs() {
        return libs;
    }

    public void setLibs(List<String> libs) {
        this.libs = libs;
    }



}

Upvotes: 1

Views: 4576

Answers (3)

Atul KS
Atul KS

Reputation: 990

resolved this problem using Jackson libs and here are my code snippets.

  **Main Class:**
       public class MainClass {

    public static void main(String[] args) throws JsonParseException, 
      JsonMappingException, IOException {

    String jsonStr = "{\r\n" + "    \"libs\": [\"ram\", \"shyam\"],\r\n" + "    \"items\": [{\r\n"
            + "         \"id\": \"001\",\r\n" + "           \"cars\": [\r\n"
            + "             \"cd1042af-856d-4649-a170-032d15a4119b\",\r\n"
            + "             \"00ed61a4-3aab-4722-90c2-7f4cca4cbded\",\r\n"
            + "             \"8fa3aa8b-3f22-4413-a41d-e78de9051de2\"\r\n" + "           ],\r\n"
            + "         \"name\": \"James\"\r\n" + "        },\r\n" + "     {\r\n"
            + "         \"id\": \"002\",\r\n" + "           \"cars\": [\r\n"
            + "             \"cd1043af-856d-4649-a170-032d15a4119b\",\r\n"
            + "             \"00ed61a4-3aab-4722-90c2-7f4cca4cbded\",\r\n"
            + "             \"8fa3aa8b-3f22-4413-a41d-e78de9051de2\"\r\n" + "           ],\r\n"
            + "         \"name\": \"James\"\r\n" + "        }\r\n" + "  ]\r\n" + "}";

    ObjectMapper mapper = new ObjectMapper();

    MyPojo details = mapper.readValue(jsonStr, MyPojo.class);

    for (String itrs : details.getLibs()) {

        System.out.println("Value for getLibs is: " + itrs);
    }

    for (Items itr : details.getItems()) {

        System.out.println("Value for getId is: " + itr.getId());
        System.out.println("Value for getName is: " + itr.getName() + "\n");

        for (String carItr : itr.getCars()) {
            System.out.println("Value for getCars is: " + carItr);
        }       }   }}

   **MyPojo** 

             import java.util.ArrayList;

     public class MyPojo {
    private ArrayList<Items> items;

private String[] libs;

public ArrayList<Items> getItems() {
    return items;
}

public void setItems(ArrayList<Items> items) {
    this.items = items;
}

public String[] getLibs() {
    return libs;
}

public void setLibs(String[] libs) {
    this.libs = libs;
  }     }

   **Items**

       public class Items {
       private String id;

       private String[] cars;

       private String name;

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String[] getCars() {
    return cars;
}

public void setCars(String[] cars) {
    this.cars = cars;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
} }   

   **RESULTS:**

  Value for getLibs is: ram
  Value for getLibs is: shyam
  Value for getId is: 001
  Value for getName is: James

   Value for getCars is: cd1042af-856d-4649-a170-032d15a4119b
   Value for getCars is: 00ed61a4-3aab-4722-90c2-7f4cca4cbded
   Value for getCars is: 8fa3aa8b-3f22-4413-a41d-e78de9051de2
   Value for getId is: 002
   Value for getName is: James

  Value for getCars is: cd1043af-856d-4649-a170-032d15a4119b
  Value for getCars is: 00ed61a4-3aab-4722-90c2-7f4cca4cbded
  Value for getCars is: 8fa3aa8b-3f22-4413-a41d-e78de9051de2

**note: I have modified the libs to add some string values to see we are good.

Upvotes: 0

Rakesh
Rakesh

Reputation: 4252

Corrected json is as follows:

{
  "libs": [],
  "items": [
    {
      "id": "001",
      "cars": [
        "cd1042af-856d-4649-a170-032d15a4119b",
        "00ed61a4-3aab-4722-90c2-7f4cca4cbded",
        "8fa3aa8b-3f22-4413-a41d-e78de9051de2"
      ],
      "name": "James"
    },
    {
      "id": "002",
      "cars": [
        "cd1043af-856d-4649-a170-032d15a4119b",
        "00ed61a4-3aab-4722-90c2-7f4cca4cbded",
        "8fa3aa8b-3f22-4413-a41d-e78de9051de2"
      ],
      "name": "James"
    }
  ]
}

Upvotes: 1

xreidenx
xreidenx

Reputation: 21

You are declaring libs as a list but using the maps/objects brackets.

Should be "libs": []

And you have extra commas after name attribute ends in each list element.

Upvotes: 2

Related Questions