【问题标题】:Jackson Array serialize and de-serializeJackson Array 序列化和反序列化
【发布时间】:2015-02-13 07:48:38
【问题描述】:

我的杰克逊字符串如下,

{
"Response":{
   "users":[{"userId":"1", "userName":"User 1"},{"userId":"2", "userName":"User 1"}]
}
}

My classes are following,

public class Response {
   private List<User> users;
// getters and setters
}

public class User{
 private String userId;
 private String userName;

//getters and setters
}

当我试图从上面的 json 字符串中获取 java 对象时。它给了我一个以下错误。知道如何解决这个问题吗?

com.fasterxml.jackson.databind.JsonMappingException: 意外令牌 (START_ARRAY),预期 START_OBJECT: 需要 JSON 对象来包含类的 As.WRAPPER_OBJECT 类型信息

【问题讨论】:

  • 你还需要一个类,例如 class ResponseJson { private Response response; }
  • 在这种情况下,您应该将响应包装在另一个对象中,以便能够反序列化此类 json。
  • 试过了。这也给了我同样的错误。
  • 你能用映射添加你的代码吗?

标签: java json serialization jackson deserialization


【解决方案1】:

您需要再添加一个类包装器:

class ResponseJson {
   @JsonProperty("Response")
   private Response response;
   // getters, setters 
}

您还需要此注释@JsonPropery,因为杰克逊对大写字段名称详细信息有一些问题:https://github.com/swagger-api/swagger-codegen/issues/249

【讨论】:

    猜你喜欢
    • 2016-12-11
    • 2014-05-19
    • 2019-08-10
    • 1970-01-01
    • 1970-01-01
    • 2013-01-01
    • 1970-01-01
    • 2019-04-03
    • 2023-03-04
    相关资源
    最近更新 更多