1

我需要一些帮助来创建将填充 GSON 解析器的类。这是自动完成 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"
}

解决方案感谢 MikO

课程是:

public class GPlacesAPIResults {

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

    @SerializedName("status")
    public String status;

}

第二:

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;

    }

第三:

public class GPlacesAPIAddress implements Serializable {

    private static final long serialVersionUID = -6916297127791361853L;

    @SerializedName("value")
    public String value;

    }

在应用程序中,我这样称呼它

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

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

谢谢你的努力:-)

4

1 回答 1

2

Result的具有属性的类locations没有任何意义......实际上我不明白您为什么想出这个,因为locations您的 JSON 中没有任何元素!

尝试这样的事情(按照您的特定符号):

Results
List<Locations> predictions;
String status;

Locations
String reference;
List <Addresses> terms;

Addresses
String value;
于 2013-05-15T22:51:16.893 回答