Java,如何解析JSON的地理位置?

时间:2013-11-10 15:23:11

标签: java json jackson

我正在尝试解析来自的回复 http://maps.googleapis.com/maps/api/geocode/json?latlng=40.183333,44.516667&sensor=true

但总是我得到空指针异常,我无法得到节点。

这是一个代码

ObjectMapper mapper = new ObjectMapper();
JsonFactory factory = mapper.getJsonFactory(); // since 2.1 use mapper.getFactory() instead
org.codehaus.jackson.JsonParser jp = factory.createJsonParser(json.toString());
JsonNode input = mapper.readTree(jp);
final JsonNode address_components = input.get("results").get("address_components");                        
System.out.println("address_components " + address_components);

for (final JsonNode element : address_components) {
    Iterator<Entry<String, JsonNode>> nodeIterator2 = element.getFields();
    while (nodeIterator2.hasNext()) {
        Map.Entry<String, JsonNode> entry = (Map.Entry<String, JsonNode>) nodeIterator2.next();
        System.out.println(entry.getKey() + " -> " + entry.getValue());
    }
}

如何找到节点?

input.get("results").get("address_components"); or input.get("address_components");

不会返回对象。

2 个答案:

答案 0 :(得分:1)

你需要检索json数组并迭代它,

JsonNode resultsArray = input.get("results");
for ( JsonNode node: resultsArray ) {
        System.out.println(node);
}

答案 1 :(得分:1)

总是,如果可以的话,我们应该创建代表我们的JSON的POJO层次结构。您的JSON来自Google,我无法相信Google团队没有为其创建Java库。如果不是,您可以使用以下POJO类:

class Root {

    private List<Result> results;
    private String status;

    //getters, setters, toString
}

class Result {

    @JsonProperty("address_components")
    private List<AddressComponent> addressComponents;

    @JsonProperty("formatted_address")
    private String formattedAddress;

    private Geometry geometry;

    private List<String> types;

    //getters, setters, toString
}

class AddressComponent {

    @JsonProperty("long_name")
    private String longName;

    @JsonProperty("short_name")
    private String shortName;

    private List<String> types;

    //getters, setters, toString
}

class Geometry {

    private Location location;

    @JsonProperty("location_type")
    private String locationType;

    @JsonProperty("viewport")
    private ViewPort viewPort;

    private ViewPort bounds;

    //getters, setters, toString
}

class ViewPort {

    private Location northeast;
    private Location southwest;

    //getters, setters, toString
}

class Location {

    private String lat;
    private String lng;

    //getters, setters, toString
}

您可以通过以下方式将JSON反序列化为上述类:

import java.net.URL;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonProgram {

    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        Root root = mapper
                .readValue(
                        new URL(
                                "http://maps.googleapis.com/maps/api/geocode/json?latlng=40.183333,44.516667&sensor=true"),
                        Root.class);
        for (Result result : root.getResults()) {
            System.out.println(result.getAddressComponents());
        }
    }
}

以上程序打印:

[AddressComponent [longName=32, shortName=32, types=[street_number]], AddressComponent [longName=Tumanyan Street, shortName=Tumanyan Street, types=[route]], AddressComponent [longName=Kentron, shortName=Kentron, types=[sublocality, political]], AddressComponent [longName=Yerevan, shortName=Yerevan, types=[locality, political]], AddressComponent [longName=Yerevan, shortName=Yerevan, types=[administrative_area_level_1, political]], AddressComponent [longName=Armenia, shortName=AM, types=[country, political]], AddressComponent [longName=0001, shortName=0001, types=[postal_code]]]
[AddressComponent [longName=Kentron, shortName=Kentron, types=[sublocality, political]], AddressComponent [longName=Yerevan, shortName=Yerevan, types=[locality, political]], AddressComponent [longName=Yerevan, shortName=Yerevan, types=[administrative_area_level_1, political]], AddressComponent [longName=Armenia, shortName=AM, types=[country, political]]]
[AddressComponent [longName=Yerevan, shortName=Yerevan, types=[locality, political]], AddressComponent [longName=Yerevan, shortName=Yerevan, types=[administrative_area_level_1, political]], AddressComponent [longName=Armenia, shortName=AM, types=[country, political]]]
[AddressComponent [longName=Yerevan, shortName=Yerevan, types=[administrative_area_level_1, political]], AddressComponent [longName=Armenia, shortName=AM, types=[country, political]]]
[AddressComponent [longName=Armenia, shortName=AM, types=[country, political]]]