gpt4 book ai didi

Java使用GSON处理一段json

转载 作者:行者123 更新时间:2023-11-30 04:34:48 27 4
gpt4 key购买 nike

我有一个 JSON 响应:

    {
"results" : [
{
"address_components" : [
{
"long_name" : "3",
"short_name" : "3",
"types" : [ "street_number" ]
},
{
"long_name" : "1033",
"short_name" : "1033",
"types" : []
},
{
"long_name" : "K osmidomkům",
"short_name" : "K osmidomkům",
"types" : [ "route" ]
},
{
"long_name" : "Praha-Suchdol",
"short_name" : "Praha-Suchdol",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "Praha",
"short_name" : "Praha",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Hlavní město Praha",
"short_name" : "Hlavní město Praha",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Hlavní město Praha",
"short_name" : "Hlavní město Praha",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Česká republika",
"short_name" : "CZ",
"types" : [ "country", "political" ]
},
{
"long_name" : "165 00",
"short_name" : "165 00",
"types" : [ "postal_code" ]
},
{
"long_name" : "Praha 620",
"short_name" : "Praha 620",
"types" : [ "postal_town" ]
}
],
"formatted_address" : "K osmidomkům 1033/3, 165 00 Praha-Praha-Suchdol, Česká republika",
"geometry" : {
"location" : {
"lat" : 50.13670170,
"lng" : 14.36865280
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 50.13805068029150,
"lng" : 14.37000178029150
},
"southwest" : {
"lat" : 50.13535271970850,
"lng" : 14.36730381970850
}
}
},
"types" : [ "street_address" ]
}
],
"status" : "OK"
}

现在我需要通过 GSON 处理 JSON,但我只需要“几何”部分。我需要位置(经纬度)。如何将这段json绑定(bind)到POJO上呢?我的意思是类似的事情

public class GoogleMapLocation
{
@SerializedName("lat")
private double latitude;

@SerializedName("lng")
private double longitude;

public double getLatitude()
{
return latitude;
}

public void setLatitude(final double latitude)
{
this.latitude = latitude;
}

public double getLongitude()
{
return longitude;
}

public void setLongitude(final double longitude)
{
this.longitude = longitude;
}
}

然后

GoogleMapLocation gml = gson.fromJson(jsonReader, GoogleMapLocation.class);

最佳答案

这应该可以工作(为了清楚起见省略了 getter、setter,...不防错):

public class GoogleMapLocation {
List<Result> results;

String status;
}

public class Result {
@SerializedName("address_components")
private List<AddressComponent> addressComponents;

@SerializedName("formatted_address")
private String formattedAddress;

private Geometry geometry;

@SerializedName("partial_match")
private String partialMatch; // not sure of the type

private List<String> types;
}

public class AddressComponents {
@SerializedName("long_name")
private String longName;

@SerializedName("short_name")
private String shortName;

private List<String> types;
}

public class Geometry {
LatLng location;

Viewport viewport;

Viewport bounds;

@SerializedName("location_type")
String locationType;
}

public class LatLng {
@SerializedName("lat");
private double latitude;

@SerializedName("lng");
private double longitude;
}

public class Viewport {
private LatLng northeast;

private LatLng southwest;
}

参见official documentation .

编辑:请注意,某些字段也可以声明为枚举(status,...)

关于Java使用GSON处理一段json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13740598/

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