JsonMappingException : START_ARRAY 토큰에서 벗어남
다음 .json 파일이 제공됩니다.
[
{
"name" : "New York",
"number" : "732921",
"center" : [
"latitude" : 38.895111,
"longitude" : -77.036667
]
},
{
"name" : "San Francisco",
"number" : "298732",
"center" : [
"latitude" : 37.783333,
"longitude" : -122.416667
]
}
]
포함 된 데이터를 표현하기 위해 두 개의 클래스를 준비했습니다.
public class Location {
public String name;
public int number;
public GeoPoint center;
}
...
public class GeoPoint {
public double latitude;
public double longitude;
}
.json 파일의 내용을 구문 분석하기 위해 Jackson 2.2.x를 사용 하고 다음 방법을 준비했습니다.
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;
}
center
속성을 생략하는 한 모든 콘텐츠를 구문 분석 할 수 있습니다. 그러나 지리 좌표를 구문 분석하려고하면 다음 오류 메시지가 표시됩니다.
com.fasterxml.jackson.databind.JsonMappingException :
[Source : android.content.res.AssetManager$AssetInputStream@416a5850;에서 START_ARRAY 토큰에서 com.example.GeoPoint의 인스턴스를 deserialize 할 수 없습니다 . 줄 : 5, 열 : 25]
(참조 체인을 통해 : com.example.Location [ "center"])
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
}
}
]
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:
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);
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
참고URL : https://stackoverflow.com/questions/19333106/jsonmappingexception-out-of-start-array-token
'development' 카테고리의 다른 글
루프 중에 TextBox.Text에 추가하면 반복 할 때마다 더 많은 메모리를 차지하는 이유는 무엇입니까? (0) | 2020.09.24 |
---|---|
Windows 스토어 앱에서 CoreDispatcher를 가져 오는 올바른 방법 (0) | 2020.09.24 |
git : 치명적 이메일 주소를 자동 감지 할 수 없음 (0) | 2020.09.24 |
Swift : 스위치 케이스의 옵션 값에 대한 테스트 (0) | 2020.09.24 |
git에서 커밋을 스쿼시한다는 것은 무엇을 의미합니까? (0) | 2020.09.24 |