Issue with parsing the content from JSON file with Jackson & message- JsonMappingException -Cannot deserialize as out of START_ARRAY token
JavaArraysJsonJacksonJava Problem Overview
Given the following .json file:
[ { "name" : "New York", "number" : "732921", "center" : [ "latitude" : 38.895111, "longitude" : -77.036667 ]
},
{
"name" : "San Francisco",
"number" : "298732",
"center" : [
"latitude" : 37.783333,
"longitude" : -122.416667
]
}
]
I prepared two classes to represent the contained data:
public class Location {
public String name;
public int number;
public GeoPoint center;
}
...
public class GeoPoint {
public double latitude;
public double longitude;
}
In order to parse the content from the .json file I use Jackson 2.2.x and prepared the following method:
public static List<Location> getLocations(InputStream inputStream) {
ObjectMapper objectMapper = new ObjectMapper();
try {
TypeFactory typeFactory = objectMapper.getTypeFactory();
CollectionType collectionType = typeFactory.constructCollectionType(
List.class, Location.class);
return objectMapper.readValue(inputStream, collectionType);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
As long as I leave out the center
property all content can be parsed. However, when I try to parse the geo-coordinates I end up with the following error message:
> com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of
> com.example.GeoPoint out of START_ARRAY token at [Source: android.content.res.AssetManager$AssetInputStream@416a5850; line: 5, column: 25]
> (through reference chain: com.example.Location["center"])
Java Solutions
Solution 1 - Java
JsonMappingException: out of START_ARRAY token
exception is thrown by Jackson object mapper as it's expecting an Object {}
whereas it found an Array [{}]
in response.
This can be solved by replacing Object
with Object[]
in the argument for geForObject("url",Object[].class)
.
References:
Solution 2 - Java
Your JSON string is malformed: the type of center
is an array of invalid objects. Replace [
and ]
with {
and }
in the JSON string around longitude
and latitude
so they will be objects:
[ { "name" : "New York", "number" : "732921", "center" : { "latitude" : 38.895111, "longitude" : -77.036667 } }, { "name" : "San Francisco", "number" : "298732", "center" : { "latitude" : 37.783333, "longitude" : -122.416667 } }]
Solution 3 - Java
I sorted this problem as verifying the json from JSONLint.com and then, correcting it. And this is code for the same.
String jsonStr = "[{\r\n" + "\"name\":\"New York\",\r\n" + "\"number\": \"732921\",\r\n"+ "\"center\": {\r\n" + "\"latitude\": 38.895111,\r\n" + " \"longitude\": -77.036667\r\n" + "}\r\n" + "},\r\n" + " {\r\n"+ "\"name\": \"San Francisco\",\r\n" +\"number\":\"298732\",\r\n"+ "\"center\": {\r\n" + " \"latitude\": 37.783333,\r\n"+ "\"longitude\": -122.416667\r\n" + "}\r\n" + "}\r\n" + "]";
ObjectMapper mapper = new ObjectMapper();
MyPojo[] jsonObj = mapper.readValue(jsonStr, MyPojo[].class);
for (MyPojo itr : jsonObj) {
System.out.println("Val of name is: " + itr.getName());
System.out.println("Val of number is: " + itr.getNumber());
System.out.println("Val of latitude is: " +
itr.getCenter().getLatitude());
System.out.println("Val of longitude is: " +
itr.getCenter().getLongitude() + "\n");
}
Note: MyPojo[].class
is the class having getter and setter of json properties.
Result:
Val of name is: New York
Val of number is: 732921
Val of latitude is: 38.895111
Val of longitude is: -77.036667
Val of name is: San Francisco
Val of number is: 298732
Val of latitude is: 37.783333
Val of longitude is: -122.416667
Solution 4 - Java
As said, JsonMappingException: out of START_ARRAY token
exception is thrown by Jackson object mapper as it's expecting an Object {}
whereas it found an Array [{}]
in response.
A simpler solution could be replacing the method getLocations
with:
public static List<Location> getLocations(InputStream inputStream) {
ObjectMapper objectMapper = new ObjectMapper();
try {
TypeReference<List<Location>> typeReference = new TypeReference<>() {};
return objectMapper.readValue(inputStream, typeReference);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
On the other hand, if you don't have a pojo like Location
, you could use:
TypeReference<List<Map<String, Object>>> typeReference = new TypeReference<>() {};
return objectMapper.readValue(inputStream, typeReference);
Solution 5 - Java
For instance:
data class
data class ToDo(
var id: Int,
var text: String?,
var completed: Boolean?) {}
Above deserialization error thrown when you use ToDo::class.java but not Array
DOEST NOT WORK
private val mapper = ObjectMapper().registerKotlinModule()
.....
val todo= mapper.readValue(response.body(), ToDo::class.java)
> com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot
> deserialize value of type com.example.api.dto.ToDo
from Array value
> (token JsonToken.START_ARRAY
) at [Source:
> (String)"[{"id":14,"text":"TEST","completed":true},{"id":13,"text":"TEST","completed":true},{"id":19,"text":"TEST","completed":true"[truncated
> 84 chars]; line: 1, column: 1]
WORK
private val mapper = ObjectMapper().registerKotlinModule()
.....
val todos = mapper.readValue(response.body(), Array<ToDo>::class.java)