gpt4 book ai didi

java - JAVA中解析googleapi地址时出错

转载 作者:行者123 更新时间:2023-12-02 03:28:29 24 4
gpt4 key购买 nike

我想解析来自此 URL 的地址

https://maps.googleapis.com/maps/api/geocode/json?address=Ctra.+Ibiza+a+San+Antonio%2C+Km+5%2C+07816+San+Rafael%2C+Illes+Balears%2C+Spain&key=YOUR_API_KEY

这个 Json:

{"results" : [ {        "address_components" : [ {                 "long_name" : "Balearic Islands",                       "short_name" : "PM",                    "types" : [ "administrative_area_level_1", "political" ] },{     "long_name" : "Spain",       "short_name" : "ES",           "types" : [ "country", "political" ]    }],     "formatted_address" : "Balearic Islands, Spain",            "geometry" : {          "bounds" : {            "northeast" : {         "lat" : 40.0945744,         "lng" : 4.3277839 },        "southwest" : {         "lat" : 38.6404833,         "lng" : 1.1572405   }},     "location" : {      "lat" : 39.5341789,         "lng" : 2.8577105 },        "location_type" : "APPROXIMATE",        "viewport" : {      "northeast" : {         "lat" : 39.9625326,         "lng" : 3.4785808 },        "southwest" : {         "lat" : 39.1207608,         "lng" : 2.3031594 }}},      "partial_match" : true,         "place_id" : "ChIJV2Jp3FqSlxIRFU2l-yEDh_8",         "types" : [ "administrative_area_level_1", "political" ]    }],     "status" : "OK" }

和这个类

public class Result {

@SerializedName("address_components")
@Expose
private List<AddressComponent> addressComponents = new ArrayList<AddressComponent>();
@SerializedName("formatted_address")
@Expose
private String formattedAddress;
@SerializedName("geometry")
@Expose
private Geometry geometry;
@SerializedName("place_id")
@Expose
private String placeId;
@SerializedName("types")
@Expose
private List<String> types = new ArrayList<String>();

/**
*
* @return
* The addressComponents
*/
public List<AddressComponent> getAddressComponents() {
return addressComponents;
}

/**
*
* @param addressComponents
* The address_components
*/
public void setAddressComponents(List<AddressComponent> addressComponents) {
this.addressComponents = addressComponents;
}

/**
*
* @return
* The formattedAddress
*/
public String getFormattedAddress() {
return formattedAddress;
}

/**
*
* @param formattedAddress
* The formatted_address
*/
public void setFormattedAddress(String formattedAddress) {
this.formattedAddress = formattedAddress;
}

/**
*
* @return
* The geometry
*/
public Geometry getGeometry() {
return geometry;
}

/**
*
* @param geometry
* The geometry
*/
public void setGeometry(Geometry geometry) {
this.geometry = geometry;
}

/**
*
* @return
* The placeId
*/
public String getPlaceId() {
return placeId;
}

/**
*
* @param placeId
* The place_id
*/
public void setPlaceId(String placeId) {
this.placeId = placeId;
}

/**
*
* @return
* The types
*/
public List<String> getTypes() {
return types;
}

/**
*
* @param types
* The types
*/
public void setTypes(List<String> types) {
this.types = types;
}

}

public class AddressComponent {

@SerializedName("long_name")
@Expose
private String longName;
@SerializedName("short_name")
@Expose
private String shortName;
@SerializedName("types")
@Expose
private List<String> types = new ArrayList<String>();

/**
*
* @return
* The longName
*/
public String getLongName() {
return longName;
}

/**
*
* @param longName
* The long_name
*/
public void setLongName(String longName) {
this.longName = longName;
}

/**
*
* @return
* The shortName
*/
public String getShortName() {
return shortName;
}

/**
*
* @param shortName
* The short_name
*/
public void setShortName(String shortName) {
this.shortName = shortName;
}

/**
*
* @return
* The types
*/
public List<String> getTypes() {
return types;
}

/**
*
* @param types
* The types
*/
public void setTypes(List<String> types) {
this.types = types;
}

}

...

我解析它

GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();

String jsonString = getGoogleMapsJsonString ();
Result result = gson.fromJson((jsonString), Result.class);

但是 result.getAddressComponents() 为空,result.getFormattedAddress() 为 null

最佳答案

您的响应 json 应反序列化为以下 dto:

public class Response {
@SerializedName("results")
@Expose
private Collection<Result> results;
@SerializedName("address_components")
@Expose
private String status;
// getters/setter
}

然后你可以获得如下结果:

    Response response = gson.fromJson((jsonString), Response.class);
Collection<Result> res = response.getResults();
for (Result result : res) {
System.out.println(result.getAddressComponents());
System.out.println(result.getFormattedAddress());
}

关于java - JAVA中解析googleapi地址时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38433128/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com