我有一个复杂的 JSON,我正在尝试使用 Jackson JSON 解析它。我对如何进入 latLng 对象以提取 lat,lng 值有点困惑。这是 JSON 的一部分:
{
"results": [
{
"locations": [
{
"latLng": {
"lng": -76.85165,
"lat": 39.25108
},
"adminArea4": "Howard County",
"adminArea5Type": "City",
"adminArea4Type": "County",
这就是我迄今为止在 Java 中所拥有的内容:
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);
已解决这就是我通过使用树模型解决问题的方式以供将来参考:
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);