i have 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 have class hold 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; } }
i'm trying convert json list of mapdata objects:
type type = new typetoken<list<mapdata>>(){}.gettype(); return gson.fromjson(jsonstring, type);
but error:
java.lang.classcastexception: com.google.gson.internal.linkedtreemap cannot cast com.deliveries.models.mapdata
what doing wrong?
i suspect method calling fromjson
returning wrong type. should return list<mapdata>
instead of mapdata
.
something like:
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); }
i have working example of issue in gist
Comments
Post a Comment