无法解析使用GSON JSON数组 [英] Unable to parse Json array using Gson

查看:172
本文介绍了无法解析使用GSON JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的JSON数组:

I have this json array:

 [
{
"id":18,
"city":"הרצליה",
"street":"החושלים 1",
"zipcode":121209,
"state":"IL",
"lat":32.158138,
"lng":34.807838
},
{
"id":19,
"city":"הרצליה",
"street":"אבא אבן 1",
"zipcode":76812,
"state":"IL",
"lat":32.161041,
"lng":34.810410
}
]

和我有这个类来保存数据:

And i have this class to hold the data:

public class MapData {
    private int id;
    private String city;
    private String street;
    private String state;
    private int zipcode;
    private double lat;
    private double lng;

    public MapData(int id, String city, String street, String state,
            int zipcode, double lat, double lng) {          
        this.id = id;
        this.city = city;
        this.street = street;
        this.state = state;
        this.zipcode = zipcode;
        this.lat = lat;
        this.lng = lng;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getStreet() {
        return street;
    }
    public void setStreet(String street) {
        this.street = street;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
    public int getZipcode() {
        return zipcode;
    }
    public void setZipcode(int zipcode) {
        this.zipcode = zipcode;
    }
    public double getLat() {
        return lat;
    }
    public void setLat(double lat) {
        this.lat = lat;
    }
    public double getLng() {
        return lng;
    }
    public void setLng(double lng) {
        this.lng = lng;
    }

}   

我试图将JSON转换成MapData对象的列表:

I'm trying to convert the json into a List of MapData objects:

Type type = new TypeToken<List<MapData>>(){}.getType();
return gson.fromJson(jsonString, type);

不过,我得到这个错误:

But i get this error:

java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.deliveries.models.MapData

我究竟做错了什么?

What am i doing wrong?

推荐答案

我怀疑这种方法调用 fromJson 将返回错误的类型。它应该返回一个名单,其中,MapData&GT; 而不是 MapData

I suspect that the method calling fromJson is returning the wrong type. It should return a List<MapData> instead of MapData.

是这样的:

public static List<MapData> getData(){
    Gson gson = new Gson();
    String jsonString = "[{\"id\":18,\"city\":\"test\",\"street\":\"test 1\",\"zipcode\":121209,\"state\":\"IL\",\"lat\":32.158138,\"lng\":34.807838},{\"id\":19,\"city\":\"test\",\"street\":\"1\",\"zipcode\":76812,\"state\":\"IL\",\"lat\":32.161041,\"lng\":34.810410}]";
    Type type = new TypeToken<List<MapData>>(){}.getType();
    return gson.fromJson(jsonString, type);     
}

我有你的问题在​​一个完全工作的例子这要点

这篇关于无法解析使用GSON JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆