Java jackson : JSON value unknown

标签 java json jackson

我仍在学习如何使用 Jackson...

所以我有一个 JSON 对象,它的值有时是整数、长字符串或列表

值:整数

{
  "id":1,
  "active":1,
  "name":"name1",
  "value":155,
  ...

值:字符串

{
  "id":2,
  "active":1,
  "name":"name2",
  "value":"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book...",
  ...

值:列表

{
  "id":3,
  "active":1,
  "name":"name3",
  "value":[
    "One",
    "Two",
    "Three",
    "Four"],
  ...

所以总的来说,它看起来像......

{
  {
      "id":1,
      "active":1,
      "name":"name1",
      "value":155,
      ...
  },
  {
      "id":2,
      "active":1,
      "name":"name2",
      "value":"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book...",
      ...
  },
  {
      "id":3,
      "active":1,
      "name":"name3",
      "value":[
        "One",
        "Two",
        "Three",
        "Four"],
      ...
  }
}

这是我的 POJO 模型

@JsonIgnoreProperties(ignoreUnknown=true)
@JsonAutoDetect(fieldVisibility= JsonAutoDetect.Visibility.ANY)
public class OQScoresRows {
  private int id;
  private int active;
  private String name;
  private List<String> value;
  ... ...

这是我的映射器代码

ObjectMapper mapper = new ObjectMapper();
try{
  POJO obj = mapper.readValue(<JSONOBJECT>, POJO.class);
}catch(JsonParseException e){
  return mapper.writeValueAsString(e);
}

问题是当我执行代码时,出现以下错误:

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of VALUE_NUMBER_INT token

我很清楚发生这种情况是因为“值”可以包含三种不同类型之一,我如何使我的代码足够灵活以适应这些类型......我总是可以在其他方法中检测该值是否是一个int、List 或 String,但我首先需要建模(不是吗)...

我的问题很简单:如何使我的代码足够灵活以适应类型......

最佳答案

如果它可以是 IntegerListString 中的任何一个,那么您可以将其声明为 Object稍后使用 instanceof 进行转换,例如:

@JsonIgnoreProperties(ignoreUnknown=true)
@JsonAutoDetect(fieldVisibility= JsonAutoDetect.Visibility.ANY)
public class OQScoresRows {
  private int id;
  private int active;
  private String name;
  private Object value;

反序列化后,您可以编写类似的逻辑:

if(value instanceof Integer){
    //do something after casting it to Integer
}else if(value instanceof List){
    //do something after casting it to List
}else if(value instanceof String){
    do something after casting it to String
}

关于Java jackson : JSON value unknown,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43543712/

相关文章:

java - Apache Commons Net FTPClient 中止文件存储

java - Kubernetes Continuous Deploy插件出现问题

java - TestNG 软断言输出不全面

javascript - 关联数组不适用于 eval() JavaScript

java - Spring MVC、JSON、长度超过 1000 个字符的字符串字段的错误序列化

java - 无法生成签名的APK?

json - Swift 4 用 ' - ' 字母解码 json

android - Android 中带有 JSON 负载的 HttpDelete 请求

json - 如何动态忽略 Jackson 序列化的属性

java - 将 Java 对象转换为 JSON 的步骤