可能であれば、常に、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]]]