我通过调用以下网址开始使用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();
}
}
当我运行程序时,我没有错误。但是如何打印解析数据?
答案 0 :(得分:2)
将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;
}
然后在您的主要内容中执行此操作:
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());
}
}
备注:强>
page
字符串传递给Gson实例Results
是复数,我认为Result
是一个更好的名字,但我在写完时离开了