While parsing a json response to Java class object using ObjectMapper, I am getting parsing JsonMappingException with message - Can not deserialize instance of com.handson.ResponseParser out of START_ARRAY token
Assuming I have below json response -
{
"statuses": [
{
"engine": "f4651cbc-a79c-4269-9711-1aeaaeff6369",
"invocations": [
{
"outputs": {
"aggrplan": {
"path": "http://someurl.com",
"repo": "S3",
"format": "application/json"
}
},
"message": "complete",
"status": "200"
}
]
}
],
"request_id": "fce46502095607ef"
}
In main method, I am doing parsing via below logic ->
ResponseParser parser = null;
if(result.length() > 0) {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);
parser = objectMapper.readValue(result, ResponseParser.class);
}
This is the class to which I want to map the json response:
@JsonRootName(value = "statuses")
public class ResponseParser {
@JsonProperty(value = "request_id")
private String requestId;
@JsonProperty(value = "statuses")
private List<Map<String, String>> statuses;
public String getRequestId() {
return requestId;
}
public void setRequestId(String requestId) {
this.requestId = requestId;
}
public List<Map<String, String>> getStatuses() {
return statuses;
}
public void setStatuses(List<Map<String, String>> statuses) {
this.statuses = statuses;
}
}
Your POJO
does not fit to JSON
payload. First of all, you do not need to DeserializationConfig.Feature.UNWRAP_ROOT_VALUE
because ResponseParser
is your root object. Secondly, instead of List<Map<String, String>>
you need to use List<Map<String, Object>>
- Map
with Object
values not with String
values. See below example:
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.util.List;
import java.util.Map;
public class JsonApp {
public static void main(String[] args) throws Exception {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.readValue(jsonFile, ResponseParser.class));
}
}
class ResponseParser {
@JsonProperty(value = "request_id")
private String requestId;
private List<Map<String, Object>> statuses;
public String getRequestId() {
return requestId;
}
public void setRequestId(String requestId) {
this.requestId = requestId;
}
public List<Map<String, Object>> getStatuses() {
return statuses;
}
public void setStatuses(List<Map<String, Object>> statuses) {
this.statuses = statuses;
}
@Override
public String toString() {
return "ResponseParser{" +
"requestId='" + requestId + '\'' +
", statuses=" + statuses +
'}';
}
}
Above code prints:
ResponseParser{requestId='fce46502095607ef', statuses=[{engine=f4651cbc-a79c-4269-9711-1aeaaeff6369, invocations=[{outputs={aggrplan={path=http://someurl.com, repo=S3, format=application/json}}, message=complete, status=200}]}]}
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.