简体   繁体   English

使用Google Places中的gson解析数据

[英]Parsing data with gson from google places

I started using the Google Places api by calling the following url 我通过调用以下网址开始使用Google Places API

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

I found a post here on stackoverflow that showed the the basic structure of classes I need to to parse the json with gson. 我在stackoverflow上找到了一篇文章,其中显示了我需要使用gson解析json的类的基本结构。 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? 但是,如何打印已解析的数据?

Add getters to your classes (and setters if you want, but are not needed) 将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;
}

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

Notes: 笔记:

  • I could not test the code completely since you posted an url and not the corresponding JSON string. 由于您发布的是网址而不是相应的JSON字符串,因此我无法完全测试代码。 It would be better to edit the question posting the JSON 最好编辑发布JSON的问题
  • You do deserialization twice, you can pass directly the page string to the Gson instance 您需要进行两次反序列化,您可以直接将page字符串传递给Gson实例
  • Results is plural, I think that Result is a better name, but I left as you wrote Results是复数形式,我认为Result是一个更好的名字,但我如您所愿

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM