从谷歌的地方GSON数据解析(Parsing data with gson from google

2019-09-27 09:40发布

我开始通过调用以下网址使用谷歌Places API的

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

我发现这里的计算器一个职位,显示出我需要解析与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();
  }
}

当我运行程序我没有得到任何错误。 但是,我该如何解析数据印刷?

Answer 1:

干将添加到您的类(和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());
    }
  }

笔记:

  • 我无法测试完全因为你发布的网址,而不是相应的JSON字符串代码。 这将是更好的编辑的问题张贴JSON
  • 你反序列化两次,你可以直接在传递page字符串到GSON实例
  • Results是复数,我认为Result是一个更好的名字,但我留给你写


文章来源: Parsing data with gson from google places
标签: java gson