简体   繁体   中英

Deserialize JSON String using org.codehaus.jackson

I am new to serializing and deserializing JSON.

I have String with value

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

which i want to deserialize, i used ObjectMapper of Jackson to deserialize

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

My deserialize Class

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;
}

}

But i am getting Error

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

By default Jackson will use property accessors in JavaBean naming conventions rather than fields. To make your example work, you can change this by adding

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

Have a look at the other methods on VisibilityChecker to customise your json mapping correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM