簡體   English   中英

Jackson JSon:無法從START_OBJECT標記中反序列化java.lang.String的實例

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

我正在使用API​​而我正在嘗試將結果反序列化,但我總是遇到同樣的錯誤。

我猜我的POJO不對,但我找不到什么錯。

錯誤在於libs

我試圖聲明為String或List而沒有任何成功。 任何想法 ?

{
"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;
    }



}

您將libs聲明為列表,但使用maps / objects括號。

應該是"libs": []

並且在每個列表元素中name屬性結束后,您有額外的逗號。

更正了json如下:

{
  "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"
    }
  ]
}

使用Jackson libs解決了這個問題,這里是我的代碼片段。

  **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

**注意:我修改了libs以添加一些字符串值,看看我們是好的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM