0

我正在尝试解析来自 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");

不返回对象。

4

2 回答 2

1

您需要检索 json 数组并对其进行迭代,

JsonNode resultsArray = input.get("results");
for ( JsonNode node: resultsArray ) {
        System.out.println(node);
}
于 2013-11-10T15:35:48.173 回答
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]]]
于 2013-11-10T23:46:45.500 回答