GSON解析google place API

时间:2013-05-15 21:38:23

标签: java json parsing gson

我需要一些帮助来创建将用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;

    }

在app中我称之为

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

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

感谢您的努力: - )

1 个答案:

答案 0 :(得分:2)

具有属性Result的{​​{1}}课程没有任何意义......实际上我不明白为什么你想出这个,因为没有locations元素JSON中的任何地方!

尝试这样的事情(遵循特定符号):

locations