There was a problem using Jackson to use JSON serialization in Java
Good morning, man!
I have a JSON string that looks like:
{
"StatusCode":0,"Message":null,"ExecutionTime":0,"ResponseData":[
{"Name":"name1","SiteId":"1234","Type":"Type1","X":"1234567","Y":"123456"},{"Name":"Name2","SiteId":"2134","Type":"Type2","Y":"1234567"},{"Name":"Name3","SiteId":"3241","Type":"Type3",{"Name":"Name4","SiteId":"4123","Type":"Type4","X":"123456","Y":"123456"}
]
}
I want to create an object that can retrieve X and Y values
I've been trying to serialize JSON strings using Jackson, but I didn't succeed I created two additional courses for Jackson The top-level classes, statuscode, message, executiontime and responsedata, look like
public class PL {
private Long statusCode;
private String executionTime;
private String message;
private ResponseData responseData;
public PL(){
}
public void setStatusCode(Long statusCode){
this.statusCode = statusCode;
}
public Long getStatusCode(){
return this.statusCode;
}
public void setExecutionTime(String executionTime){
this.executionTime = executionTime;
}
public String getExecutionTime(){
return this.executionTime;
}
public void setMessage(String message){
this.message = message;
}
public String getMessage(){
return this.message;
}
public void setResponseData(ResponseData responseData){
this.responseData = responseData;
}
public ResponseData getResponseData(){
return this.responseData;
}
}
Where reponsedata is returned as an object, and then I have another class for serializing responsedata, which looks like
public class ResponseData {
private String name;
private String siteId;
private String type;
private String x;
private String y;
public ResponseData(){
}
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void setSiteId(String siteId){
this.siteId = siteId;
}
public String getSiteId(){
return this.siteId;
}
public void setType(String type){
this.type = type;
}
public String setType(){
return this.type;
}
public void setX(String x){
this.x = x;
}
public String getX(){
return this.x;
}
public void setY(String y){
this.y = y;
}
public String getY(){
return this.y;
}
}
Then I create an objectmapper
private final static ObjectMapper mapper = new ObjectMapper();
And try to read the value in this way
ResponseData e = mapper.readValue(result.toString(),ResponseData.class);
And eventually get an exception
It seems that it cannot resolve the first entry statusmessage Even if I delete the second class and only try to parse the first four entries, I return responsedata as a string, and I still get the same exception
Solution
First, you should have a list < responsedata > in PL, which is not a simple responsedata attribute As you can see, in JSON, responsedata is an array "responsedata": [...] so it will be deserialized to list Each element of the list is a responsedata object that you define
Then you encounter a case problem. You don't have uppercase JSON in the class attribute You can use @ jsonproperty (see API) annotation to overcome this problem, as follows:
class PL {
@JsonProperty("StatusCode")
private Long statusCode;
@JsonProperty("ExecutionTime")
private String executionTime;
@JsonProperty("Message")
private String message;
@JsonProperty("ResponseData")
private List<ResponseData> responseDatas;
public PL(){
}
// getters/Setters
}
class ResponseData {
@JsonProperty("Name")
private String name;
@JsonProperty("SiteId")
private String siteId;
@JsonProperty("Type")
private String type;
@JsonProperty("X")
private String x;
@JsonProperty("Y")
private String y;
public ResponseData(){
}
// getters/Setters
}
Then read your JSON as a pl object, as follows:
ObjectMapper mapper = new ObjectMapper();
PL pl = mapper.readValue(json,PL.class);
for(ResponseData rd : pl.getResponseDatas()) {
System.out.println(rd.getX());
System.out.println(rd.getY());
}
This output:
1234567 123456 1234567 1234567 1234567 1234567 123456 123456
