Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用来自google places的gson解析数据_Java_Gson - Fatal编程技术网

Java 使用来自google places的gson解析数据

Java 使用来自google places的gson解析数据,java,gson,Java,Gson,我通过调用以下url开始使用GooglePlacesAPI https://maps.googleapis.com/maps/api/place/textsearch/json?query=restaurants+in+Sydney&sensor=true&key=AddYourOwnKeyHere 我在这里找到了一篇关于stackoverflow的帖子,它展示了我需要用gson解析json的类的基本结构。这是我到目前为止所拥有的 public class GoogleMapp

我通过调用以下url开始使用GooglePlacesAPI

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

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

向类中添加getter(如果需要,也可以添加setter,但不需要)

公共类GoogleMapper{
@SerializedName(“调试信息”)
私有列表调试信息;
@SerializedName(“html_属性”)
私有列表html_属性;
@SerializedName(“下一页\u标记”)
私有字符串下一页\u令牌;
@SerializedName(“结果”)
私人名单结果;
公共列表getDebugInfo(){
返回调试信息;
}
公共列表getHtmlAttributes(){
返回html_属性;
}
公共字符串getNextPageToken(){
返回下一页\u令牌;
}
公共列表getResults(){
返回结果;
}
公共字符串getStatus(){
返回状态;
}
@序列化名称(“状态”)
私有字符串状态;
}

公共类结果{
公共字符串getFormattedAddress(){
返回格式化的地址;
}
公共字符串getIcon(){
返回图标;
}
公共字符串getId(){
返回id;
}
公共字符串getName(){
返回名称;
}
公共双重评级(){
回报率;
}
公共字符串getReference(){
返回参考;
}
公共列表getTypes(){
返回类型;
}
@SerializedName(“格式化的地址”)
私有字符串格式的地址;
@序列化名称(“图标”)
私有字符串图标;
@序列化名称(“id”)
私有字符串id;
@序列化名称(“名称”)
私有字符串名称;
@序列化名称(“评级”)
私人双重评级;
@序列化名称(“引用”)
私有字符串引用;
@序列化名称(“类型”)
私有列表类型;
}
然后在你的主要任务中这样做:

  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());
    }
  }
Gson Gson=new GsonBuilder().serializeNulls().create();
GoogleMapper mapper=gson.fromJson(第页,GoogleMapper.class);
List results=mapper.getResults();
如果(结果!=null){
对于(结果r:结果){
System.out.println(“获取的名称:+r.getName());
}
}
注意事项:

  • 我无法完全测试代码,因为您发布了一个url,而不是相应的JSON字符串。最好编辑发布JSON的问题
  • 如果反序列化两次,则可以直接将
    页面
    字符串传递给Gson实例
  • Results
    是复数,我认为
    Result
    是一个更好的名字,但我还是像你写的那样离开了

你是说打印在控制台上吗?@giampaolo是的,我想知道如何访问数据。
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());
    }
  }