小编use*_*784的帖子

使用Jackson ACCEPT_SINGLE_VALUE_AS_ARRAY从String反序列化ArrayList

问题使用Jackson 2将数组反序列化为字符串

这是使用Jackson从String反序列化ArrayList的类似问题

传入的JSON(我无法控制)有一个元素'thelist',它是一个数组.但是,有时它会以空字符串而不是数组形式出现:

例如.而不是"thelist":[]
它以"thelist"形式出现:""

我在解析这两种情况时遇到了麻烦.

"sample.json"文件的正常工作:

{
   "name" : "widget",
   "thelist" : 
    [
       {"height":"ht1","width":"wd1"}, 
       {"height":"ht2","width":"wd2"}
    ]
}
Run Code Online (Sandbox Code Playgroud)

课程:

public class Product { 
    private String name; 
    private List<Things> thelist; 
    // with normal getters and setters not shown
}

public class Things {
        String height;
        String width;
        // with normal getters and setters not shown
}
Run Code Online (Sandbox Code Playgroud)

工作正常的代码:

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Test2 {
 public static void main(String[] args) …
Run Code Online (Sandbox Code Playgroud)

serialization json jackson

9
推荐指数
1
解决办法
2万
查看次数

标签 统计

jackson ×1

json ×1

serialization ×1