简体   繁体   English

Java:Jackson String to Json-无法反序列化START_ARRAY令牌之外的实例

[英]Java: Jackson String to Json - Can not deserialize instance of out of START_ARRAY token

Getting straight to it... 直奔它...

I have a method that returns a string (see example string below) - essentially, I make a HTTP GET Request to a URL and the response is the string below... 我有一个返回字符串的方法(请参见下面的示例字符串)-本质上,我向URL发出HTTP GET请求,响应是下面的字符串...

{
  "total": 30,
  "rows": [
    {
      "id": 1,
      "parent": "parentA",
      "children": "childB, childC, childD"
    },
    {
      "id": 2,
      "parent": "parentE",
      "children": "childF, childG, childH"
    },
    {
      "id": 3,
      "parent": "parentI",
      "children": "childJ, childK, childL"
    },
    {
      "id": 4,
      "parent": "parentM",
      "children": "childN, childO"
    },
    {
      "id": 5,
      "parent": "parentP",
      "children": "childQ, childR, childS, childT"
    },
    {
      "id": 6,
      "parent": "parentU",
      "children": "childV, childW, childX"
    },
    {
      "id": 7,
      "parent": "parentY",
      "children": "childZ"
    }
  ]
}

I then assign this string to a variable, then map it to my model... 然后,我将此字符串分配给变量,然后将其映射到我的模型...

String strRel = <JSON OBJECT FROM ABOVE>
ObjectMapper mapper = new ObjectMapper();
MyModel obj = mapper.readValue(strRel, MyModel.class);

However, when I run my code, it, unfortunately, returns the following error... 但是,不幸的是,当我运行代码时,它返回以下错误...

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.my.models.MyModel out of START_ARRAY token

Ultimately, I know what is causing the error, it's the Array of Objects, "rows"...but I am not sure how to fix it. 最终,我知道是什么导致了错误,这是对象数组,“行” ...但是我不确定如何解决它。 Obviously, I cannot change the schema of the string/object coming back to me. 显然,我无法更改返回给我的字符串/对象的架构。

Any advice will be greatly appreciated. 任何建议将不胜感激。

UPDATE: MyModel 更新: MyModel

public class MyModel {
    public MyModel() {}

    private int total;
    private ModelRows rows;

    public int getTotal() { return total; }
    public ModelRows getRows() { return rows; }
}

UPDATE: ModelRows 更新: ModelRows

public class ModelRows {
    public ModelRows() {}

    private int id;
    private String parent;
    private String children;

    public int getId() { return id; }

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

    public String getParent() { return parent; }

    public void setParent(String parent) { this.parent = parent; }

    public String getChildren() { return children; }

    public void setChildren(String children) { this.children = children; }
}

Made below changes in your MyModel class 在MyModel类中进行以下更改

public class MyModel {
    public MyModel() {}

    private int total;
    private List<ModelRows> rows;

    public int getTotal() { return total; }
    public List<ModelRows> getRows() { return rows; }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 杰克逊数组或字符串:无法从START_ARRAY令牌中反序列化java.lang.String实例 - Jackson array or string: Can not deserialize instance of java.lang.String out of START_ARRAY token Codehaus Jackson JSON数据转换为POJO无法反序列化实例...超出START_ARRAY令牌 - Codehaus Jackson JSON data to POJO Can not deserialize instance … out of START_ARRAY token Jackson 错误:无法从 START_ARRAY 令牌反序列化 `java.lang.String` 的实例 - Jackson error: Cannot deserialize instance of `java.lang.String` out of START_ARRAY token 使用Jackson反序列化JSON时出错:无法从START_ARRAY令牌中反序列化java.util.LinkedHashMap实例 - Error while deserializing JSON using Jackson : Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token JSON到XML转换中的错误:无法从START_ARRAY令牌中反序列化java.util.HashMap的实例 - error in JSON to XML conversion: Can not deserialize instance of java.util.HashMap out of START_ARRAY token 无法读取文档:无法从START_ARRAY令牌中反序列化java.lang.String实例 - Could not read document: Can not deserialize instance of java.lang.String out of START_ARRAY token 无法从START_ARRAY令牌中反序列化java.lang.String实例; - Can not deserialize instance of java.lang.String out of START_ARRAY token; 无法从 START_ARRAY 令牌中反序列化 java.lang.String 的实例 - Can not deserialize instance of java.lang.String out of START_ARRAY token 用杰克逊反序列化json对象的对象列表-无法从start_array令牌反序列化实例 - deserializing a list of objects json object with jackson - cannot deserialize instance out of start_array token com.fasterxml.jackson.databind.exc.MismatchedInputException:无法从 START_ARRAY 令牌中反序列化对象实例 - JAVA - com.fasterxml.jackson.databind.exc.MismatchedInputException: Can not deserialize instance of object out of START_ARRAY token - JAVA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM