0

我是序列化和反序列化 JSON 的新手。

我有值的字符串

String json = [{"Area":"LoremIpsum","Quantity":1500,"isSubArea":false},
{"Area":"LoremIpsum","Quantity":700,"isSubArea":false}];

我想反序列化,我使用杰克逊的 ObjectMapper 反序列化

ObjectMapper mapper = new ObjectMapper();
List<DeserializeJSON> agentsList = mapper.readValue(json,new TypeReference<List<DeserializeJSON>>() {});

我的反序列化类

public class DeserializeJSON {
String Area;
Integer Quantity = 0;
boolean isSubArea = false;

public String getArea() {
    return Area;
}
public void setArea(String Area) {
    this.Area = Area;
}
public Integer getQuantity() {
    return Quantity;
}
public void setQuantity(Integer Quantity) {
    this.Quantity = Quantity;
}
public boolean isSubArea() {
    return isSubArea;
}
public void setSubArea(boolean isSubArea) {
    this.isSubArea = isSubArea;
}

}

但我收到错误

Unrecognized field "Area" (Class DeserializeJSON), not marked as ignorable at [Source: java.io.StringReader@2acf7a; line: 1, column: 11] (through reference chain: DeserializeJSON["Area"])
4

1 回答 1

2

默认情况下,Jackson 将在JavaBean 命名约定中使用属性访问器而不是字段。为了使您的示例正常工作,您可以通过添加来更改它

mapper.setVisibilityChecker(mapper.getVisibilityChecker().withFieldVisibility(Visibility.ANY));

VisibilityChecker查看正确自定义 json 映射的其他方法。

于 2013-04-10T07:34:51.330 回答