总是,如果可以的话,我们应该创建代表我们 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]]]