Reputation: 635
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"])
Upvotes: 0
Views: 900
Reputation: 5127
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.
Upvotes: 2