使用gson从Google地方解析数据 [英] Parsing data with gson from google places

查看:191
本文介绍了使用gson从Google地方解析数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过调用以下网址开始使用Google Places api

I started using the Google Places api by calling the following url

https://maps.googleapis.com/maps/api/place/textsearch/json?query=restaurants+in+Sydney&sensor=true&key=AddYourOwnKeyHere



我在这里找到了一个关于stackoverflow的帖子,它显示了我需要使用gson解析json的类的基本结构。

I found a post here on stackoverflow that showed the the basic structure of classes I need to to parse the json with gson. Here is what I have so far.

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;
}

以下是主要方法

And here is the main method

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();
  }
}

当我运行程序时,我没有遇到任何错误。但是,如何获得解析的数据打印?

When I run the program I get no errors. But how do I get the parsed data printed?

推荐答案

将getters添加到您的类(如果需要, )

Add getters to your classes (and setters if you want, but are not needed)

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;
}

然后在你的主页中这样做:

and then do like this in your 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());
    }
  }

注意:


  • 由于您发布了网址而不是相应的JSON字符串,因此我无法完全测试代码。编辑发布JSON的问题会更好

  • 您可以执行两次反序列化,您可以直接将页面字符串传递给Gson实例

  • 结果是复数,我认为 Result 是一个更好的名字,但是我在您写下

  • I could not test the code completely since you posted an url and not the corresponding JSON string. It would be better to edit the question posting the JSON
  • You do deserialization twice, you can pass directly the page string to the Gson instance
  • Results is plural, I think that Result is a better name, but I left as you wrote

这篇关于使用gson从Google地方解析数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆