我通过调用以下网址开始使用 Google Places api
https://maps.googleapis.com/maps/api/place/textsearch/json?query=restaurants+in+Sydney&sensor=true&key=AddYourOwnKeyHere
我在 stackoverflow 上找到了一篇文章,其中展示了我需要使用 gson 解析 json 的类的基本结构。这是我到目前为止所拥有的。
public class GoogleMapper
{
@SerializedName("debug_info")
private List<String> debug_info;
@SerializedName("html_attributions")
private List<String> html_attributions;
@SerializedName("next_page_token")
private String next_page_token;
@SerializedName("results")
private List<Results> results;
@SerializedName("status")
private String status;
}
public class Results
{
@SerializedName("formatted_address")
private String formatted_address;
@SerializedName("icon")
private String icon;
@SerializedName("id")
private String id;
@SerializedName("name")
private String name;
@SerializedName("rating")
private Double rating;
@SerializedName("reference")
private String reference;
@SerializedName("types")
private List<String> types;
}
这是主要方法
public static void main(String[] args)
{
Places p = new Places();
try
{
String page = p.getPage();
Gson gson = new GsonBuilder().serializeNulls().create();
JsonParser parser = new JsonParser();
JsonObject o = (JsonObject)parser.parse(page);
String json = gson.toJson(o);
GoogleMapper mapper = gson.fromJson(json, GoogleMapper.class);
}
catch (Exception e)
{
e.printStackTrace();
}
}
当我运行该程序时,没有出现任何错误。但是如何打印解析后的数据呢?
最佳答案
将 getter 添加到您的类中(如果需要,也可以添加 setter,但不需要)
public class GoogleMapper {
@SerializedName("debug_info")
private List<String> debug_info;
@SerializedName("html_attributions")
private List<String> html_attributions;
@SerializedName("next_page_token")
private String next_page_token;
@SerializedName("results")
private List<Results> results;
public List<String> getDebugInfo() {
return debug_info;
}
public List<String> getHtmlAttributions() {
return html_attributions;
}
public String getNextPageToken() {
return next_page_token;
}
public List<Results> getResults() {
return results;
}
public String getStatus() {
return status;
}
@SerializedName("status")
private String status;
}
<小时/>
public class Results {
public String getFormattedAddress() {
return formatted_address;
}
public String getIcon() {
return icon;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public Double getRating() {
return rating;
}
public String getReference() {
return reference;
}
public List<String> getTypes() {
return types;
}
@SerializedName("formatted_address")
private String formatted_address;
@SerializedName("icon")
private String icon;
@SerializedName("id")
private String id;
@SerializedName("name")
private String name;
@SerializedName("rating")
private Double rating;
@SerializedName("reference")
private String reference;
@SerializedName("types")
private List<String> types;
}
然后在你的 main 中这样做:
Gson gson = new GsonBuilder().serializeNulls().create();
GoogleMapper mapper = gson.fromJson(page, GoogleMapper.class);
List<Results> results = mapper.getResults();
if(results != null){
for(Results r : results){
System.out.println("Fetched name: " + r.getName());
}
}
注释:
- 由于您发布了 url 而不是相应的 JSON 字符串,因此我无法完全测试代码。最好编辑发布 JSON 的问题
- 反序列化两次,可以直接将
page
字符串传递给Gson实例 Results
是复数,我认为Result
是一个更好的名字,但我按照你写的留下了
关于java - 使用 gson 解析来自 google 地点的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20037251/