问题使用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)