Java 使用Jackson将JSON数组反序列化为具有私有列表属性的对象
类似以下内容的JSON字符串:Java 使用Jackson将JSON数组反序列化为具有私有列表属性的对象,java,json,serialization,jackson,deserialization,Java,Json,Serialization,Jackson,Deserialization,类似以下内容的JSON字符串: [ "a", "b", "c" ] 通常会反序列化到列表。但我有一个类似这样的类: public class Foo { private List<String> theList; public Foo(List<String> theList) { this.theList = theList; } public String toString() { return new ObjectM
[
"a", "b", "c"
]
通常会反序列化到列表
。但我有一个类似这样的类:
public class Foo {
private List<String> theList;
public Foo(List<String> theList) {
this.theList = theList;
}
public String toString() {
return new ObjectMapper().writeValueAsString(theList);
}
// ... more methods
}
这怎么可能
我已经尝试将@JsonCreator
与构造函数一起使用,但始终得到:
JsonMappingException: Can not deserialize instance of ... out of START_ARRAY token
对于Jackson 2.4.3,这个
@JsonCreator
public Foo(List<String> theList) {
this.theList = theList;
}
...
String jsonString = "[\"a\", \"b\", \"c\"]";
Foo foo = new ObjectMapper().readValue(jsonString, Foo.class);
System.out.println(foo.getTheList());
对于Jackson 2.4.3,这个
@JsonCreator
public Foo(List<String> theList) {
this.theList = theList;
}
...
String jsonString = "[\"a\", \"b\", \"c\"]";
Foo foo = new ObjectMapper().readValue(jsonString, Foo.class);
System.out.println(foo.getTheList());
你是对的,我混淆了com.fasterxml.jackson和org.codehouse.jackson导入:-(你是对的,我混淆了com.fasterxml.jackson和org.codehouse.jackson导入:-(
[a, b, c]