0

I am trying to parse response from http://maps.googleapis.com/maps/api/geocode/json?latlng=40.183333,44.516667&sensor=true

But always I get null pointer exceptions, I could not get nodes.

here is a code

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());
    }
}

How can I find nodes?

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

does not return object.

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 に答える