gpt4 book ai didi

java - 谷歌地点 API 的 GSON 解析

转载 作者:行者123 更新时间:2023-11-30 04:22:55 26 4
gpt4 key购买 nike

我需要一些帮助来创建将填充 GSON 解析器的类。这是自动完成 Google Places API 的输出:

{
"predictions" : [
{
"reference" : "CjQtAA",
"terms" : [
{
"offset" : 0,
"value" : "Ladimirevci"
},
{
"offset" : 13,
"value" : "Hrvatska"
}
],
"types" : [ "locality", "political", "geocode" ]
},
{
"reference" : "CjQtAAA",
"terms" : [
{
"offset" : 0,
"value" : "Ladimirevci"
},
{
"offset" : 13,
"value" : "Hrvatska"
}
],
"types" : [ "locality", "political", "geocode" ]
}
],
"status" : "OK"
}

解决方案感谢 MikO

类(class)有:

public class GPlacesAPIResults {

@SerializedName("predictions")
public List<GPlacesAPILocation> predictions;

@SerializedName("status")
public String status;

}

第二:

public class GPlacesAPILocation implements Serializable {

private static final long serialVersionUID = 4509808527882750586L;

@SerializedName("reference")
private String reference;

@SerializedName("terms")
private List<GPlacesAPIAddress> terms;

@SerializedName("types")
private List<String> types;

}

第三:

public class GPlacesAPIAddress implements Serializable {

private static final long serialVersionUID = -6916297127791361853L;

@SerializedName("value")
public String value;

}

在应用程序中我这样调用它

InputStreamReader in = new InputStreamReader(conn.getInputStream()); //results from places api

GPlacesAPIResults lcs = new Gson().fromJson( in , GPlacesAPIResults.class);

感谢您的努力:-)

最佳答案

你的带有属性locationsResult类没有任何意义......事实上我不明白你为什么想出这个,因为没有JSON 中任意位置的 locations 元素!

尝试这样的事情(遵循您的特定符号):

Results
List<Locations> predictions;
String status;

Locations
String reference;
List <Addresses> terms;

Addresses
String value;

关于java - 谷歌地点 API 的 GSON 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16575405/

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