我需要一些帮助来创建将由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
类是:
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);
谢谢您的努力:-)
最佳答案
您的具有属性Result
的locations
类没有任何意义...实际上,我不明白您为什么要这么做,因为JSON中的任何地方都没有locations
元素!
尝试这样的操作(遵循您的特定符号):
Results
List<Locations> predictions;
String status;
Locations
String reference;
List <Addresses> terms;
Addresses
String value;