잭슨 JSON을 사용하여 구문 분석하려고하는 복잡한 JSON이 있습니다. latLng 객체로 들어가서 lat, lng 값을 꺼내는 방법에 대해서는 약간 혼란 스럽습니다. 이것은 내가 그것을 꺼내 지금까지 자바가 무엇Jackson JSON 파서 사용 : 복잡한 JSON?
{
"results": [
{
"locations": [
{
"latLng": {
"lng": -76.85165,
"lat": 39.25108
},
"adminArea4": "Howard County",
"adminArea5Type": "City",
"adminArea4Type": "County",
: 이것은 JSON의 일부입니다
public class parkJSON
{
public latLng _latLng;
public static class latLng
{
private String _lat, _lng;
public String getLat() { return _lat; }
public String getLon() { return _lng; }
}
}
및 해결
이ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
parkJSON geo = mapper.readValue(parse, parkJSON.class);
System.out.println(mapper.writeValueAsString(geo));
String lat = geo._latLng.getLat();
String lon = geo._latLng.getLon();
output = lat + "," + lon;
System.out.println("Found Coordinates: " + output);
이것은 어떻게 미래의 참조를 위해 Tree Model을 사용하여 문제를 해결했습니다.
ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JsonNode rootNode = mapper.readTree(parse);
JsonNode firstResult = rootNode.get("results").get(0);
JsonNode location = firstResult.get("locations").get(0);
JsonNode latLng = location.get("latLng");
String lat = latLng.get("lat").asText();
String lng = latLng.get("lng").asText();
output = lat + "," + lng;
System.out.println("Found Coordinates: " + output);
트리 모델 방법을 사용하여 작업했습니다. –
ObjectMapper mapper = new ObjectMapper(); // 전역으로 재사용하거나 공유 할 수 있습니다. mapper.configure (DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); JsonNode rootNode = mapper.readTree (구문 분석); JsonNode firstResult = rootNode.get ("results"). get (0); JsonNode location = firstResult.get ("locations"). get (0); JsonNode latLng = location.get ("latLng"); String lat = latLng.get ("lat"). asText(); String lng = latLng.get ("lng"). asText(); –
정말 대단합니다 :-) 프로젝트에 행운이 있기를 바랍니다. – fvu