zoukankan      html  css  js  c++  java
  • 解决 Jackson反序列化 Unexpected token ... , expected VALUE_STRING: need JSON String that contains type id (for subtype of ...)

    首先检查是否是 objectMapper.enableDefaultTyping(); 的受害者。优先考虑删除该配置。

    使用Jackson把数组的json字符串反序列化为List时候报了个JsonMappingException。

    java.lang.UnsupportedOperationException: com.fasterxml.jackson.databind.JsonMappingException: Unexpected token (START_OBJECT), expected VALUE_STRING: need JSON String that contains type id (for subtype of java.util.List)
    at [Source: [ ......

    找到问题代码,粗看似乎没什么问题?

    List<MyObject> objectList = objectMapper.readValue(jsonString, new TypeReference<List<MyObject>>() {}); //jsonString是个json对象的数组

    注意到异常信息“need JSON String that contains type id (for subtype of java.util.List)”。想了一会儿,好吧,有答案了。

    List<MyObject> objectList = objectMapper.readValue(jsonString, new TypeReference<ArrayList<MyObject>>() {}); //jsonString是个json对象的数组

    其实,有一种比较老派的反序列化为List的方式...

    List<MyObject> objectList = Arrays.asList(objectMapper.readValue(jsonString, MyObject[].class)); //jsonString是个json对象的数组

    当对一些较复杂的对象进行反序列化时,例如拥有接口类型成员变量的类。举个栗子:

    @Data
    public class TypeValue {
      private Integer type;
      private List<Integer> value;
    }

    有上面这个类,需要把json字符串反序列化成 Map<String, TypeValue> 这样的对象,怎么做?

    可以在TypeValue这个类中使用 @JsonCreator 注解解决。

    @Data
    public class TypeValue {
      private Integer type;
      private List<Integer> value;
    
      @JsonCreator    //为Jackson提供构造函数
      public TypeValue(@JsonProperty("type") final Integer type, @JsonProperty("value") final int[] value) {
        this.type= type;
        this.value = Ints.asList(value);
      }
    }

    Jackson能够把[]数组转换为List。因此可以用以上方法绕过Jackson的限制。

    以上使用了guava和lombok

  • 相关阅读:
    POJ 1426 Find The Multiple(数论——中国同余定理)
    POJ 2253 Frogger(Dijkstra变形——最短路径最大权值)
    POJ 3790 最短路径问题(Dijkstra变形——最短路径双重最小权值)
    POJ 3278 Catch That Cow(模板——BFS)
    HDU 1071 The area
    HDU 1213 How Many Tables(模板——并查集)
    POJ 1611 The Suspects
    light oj 1214 Large Division
    POJ 1258 Agri-Net(Prim算法求解MST)
    POJ 2387 Til the Cows Come Home(模板——Dijkstra算法)
  • 原文地址:https://www.cnblogs.com/niceboat/p/7284099.html
Copyright © 2011-2022 走看看