Jackson & message - Json Mapping Exception - 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
property 모든 콘텐츠를 구문 분석할 수 있습니다.다만, 지오 좌표를 해석하려고 하면, 다음의 에러 메세지가 표시됩니다.
com.sysml.syslog.syslogind.syslogindJson Mapping Exception:의 인스턴스를 역직렬화할 수 없습니다.
com.discloss.conf를 클릭합니다.[출처: Android.content.res]의 START_ARRAY 토큰에서 GeoPoint가 제외되었습니다.자산관리자$자산Input Stream @ 416a5850, 줄: 5, 열: 25]
(레퍼런스 체인: com.display 경유).장소 ["중앙"])
JsonMappingException: out of START_ARRAY token
잭슨 오브젝트 맵퍼에 의해 예외가 느려지는 것은 잭슨 오브젝트 맵퍼에 의해Object {}
반면, 그것은Array [{}]
응답으로.
이 문제는 교환을 통해 해결할 수 있습니다.Object
와 함께Object[]
을 위한 논의에서geForObject("url",Object[].class)
참고 자료:
JSON 문자열의 형식이 잘못되었습니다.center
는 비활성 객체의 배열입니다.교체하다[
그리고.]
와 함께{
그리고.}
에워싸고 있는 JSON 스트링으로longitude
그리고.latitude
오브젝트가 됩니다.
[
{
"name" : "New York",
"number" : "732921",
"center" : {
"latitude" : 38.895111,
"longitude" : -77.036667
}
},
{
"name" : "San Francisco",
"number" : "298732",
"center" : {
"latitude" : 37.783333,
"longitude" : -122.416667
}
}
]
저는 이 문제를 JSONLint.com에서 json을 확인하고 수정하는 것으로 분류했습니다.그리고 이것은 같은 코드입니다.
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");
}
주의:MyPojo[].class
json 속성의 getter 및 setter를 가진 클래스입니다.
결과:
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
말씀드렸듯이JsonMappingException: out of START_ARRAY token
잭슨 오브젝트 맵퍼에 의해 예외가 느려지는 것은 잭슨 오브젝트 맵퍼에 의해Object {}
반면, 그것은Array [{}]
응답으로.
보다 간단한 해결책은 이 방법을 대체하는 것일 수 있습니다.getLocations
포함:
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;
}
반면에 포조가 없으면Location
, 다음을 사용할 수 있습니다.
TypeReference<List<Map<String, Object>>> typeReference = new TypeReference<>() {};
return objectMapper.readValue(inputStream, typeReference);
예:
데이터 클래스
data class ToDo(
var id: Int,
var text: String?,
var completed: Boolean?) {}
위의 deserialization 오류는 ToDo::class.java를 사용하고 어레이::class.java를 json 목록에 사용하지 않을 때 발생합니다.
동작하지 않다
private val mapper = ObjectMapper().registerKotlinModule()
.....
val todo= mapper.readValue(response.body(), ToDo::class.java)
com.sysml.syscl.syscind.exc를 지정합니다.MismatchedInputException: 유형 값을 역직렬화할 수 없습니다.
com.example.api.dto.ToDo
어레이 값에서(토큰)JsonToken.START_ARRAY
) [출처 : ( String ) ]"[ { " id " : 14 , text " : TEST " , completed " : { " id " : 13 , text " " " " " text " " " 19 , text " " " " " " " " " : " true , 1 " 행 : 1 " ) 。
일하다.
private val mapper = ObjectMapper().registerKotlinModule()
.....
val todos = mapper.readValue(response.body(), Array<ToDo>::class.java)
언급URL : https://stackoverflow.com/questions/19333106/issue-with-parsing-the-content-from-json-file-with-jackson-message-jsonmappin
'programing' 카테고리의 다른 글
타이프스크립트 내보내기 vs. 기본 내보내기 (0) | 2022.09.25 |
---|---|
Windows에서 Python script as a Service를 실행하는 방법은 무엇입니까? (0) | 2022.09.25 |
Java에서 목록으로 맵을 변환하는 방법 (0) | 2022.08.31 |
vue.js axios POST에서 Laravel로 JSON을 입수하려면 어떻게 해야 합니까? (0) | 2022.08.31 |
DESTDIR 및 제조원 접두사 (0) | 2022.08.31 |