简体   繁体   English

Google Places API的GSON解析

[英]GSON parsing of google places API

I need a little help with creating classes which will be filled with GSON parser. 我需要一些帮助来创建将由GSON解析器填充的类。 This is the output od autocomplete Google Places API: 这是自动完成的Google Places API的输出:

{
   "predictions" : [
  {
     "reference" : "CjQtAA",
     "terms" : [
        {
           "offset" : 0,
           "value" : "Ladimirevci"
        },
        {
           "offset" : 13,
           "value" : "Hrvatska"
        }
     ],
     "types" : [ "locality", "political", "geocode" ]
  },
  {
     "reference" : "CjQtAAA",
     "terms" : [
        {
           "offset" : 0,
           "value" : "Ladimirevci"
        },
        {
           "offset" : 13,
           "value" : "Hrvatska"
        }
     ],
     "types" : [ "locality", "political", "geocode" ]
  }
],
  "status" : "OK"
}

SOLUTION thanks to MikO 解决方案感谢MikO

Classes are: 类是:

public class GPlacesAPIResults {

    @SerializedName("predictions")
    public List<GPlacesAPILocation> predictions;

    @SerializedName("status")
    public String status;

}

Second: 第二:

public class GPlacesAPILocation implements Serializable {

    private static final long serialVersionUID = 4509808527882750586L;

    @SerializedName("reference")
    private String reference;

    @SerializedName("terms")
    private List<GPlacesAPIAddress> terms;

    @SerializedName("types")
    private List<String> types;

    }

Third: 第三:

public class GPlacesAPIAddress implements Serializable {

    private static final long serialVersionUID = -6916297127791361853L;

    @SerializedName("value")
    public String value;

    }

In app I call it like this 在应用中,我这样称呼它

InputStreamReader in = new InputStreamReader(conn.getInputStream()); //results from places api

GPlacesAPIResults lcs = new Gson().fromJson( in , GPlacesAPIResults.class);

Thank you for the effort :-) 谢谢您的努力:-)

Your Result class with an attribute locations doesn't make any sense... in fact I don't understand why you came up with that, since there's no locations element anywhere in your JSON! 您的带有属性locations Result类没有任何意义...实际上,我不明白您为什么要这么做,因为JSON中没有locations元素!

Try something like this (following your particular notation ): 尝试这样的操作(遵循您的特殊记法 ):

Results
List<Locations> predictions;
String status;

Locations
String reference;
List <Addresses> terms;

Addresses
String value;

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

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