Ich habe dieses json Array analysieren:kann nicht Json-Array mit Gson
[
{
"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
}
]
Und ich habe diese Klasse halten Sie die Daten:
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;
}
}
Ich versuche zu konvertieren die json in ein Liste der MapData Objekte:
Type type = new TypeToken<List<MapData>>(){}.getType();
return gson.fromJson(jsonString, type);
Aber ich bekomme diese Fehlermeldung:
java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.deliveries.models.MapData
Was mache ich falsch?
Typ. Gson weiß, dass er java.util.List deserialisieren muss, hat aber keine Ahnung, welche Objekte er hineinlegen soll, also benutzt er interne Typen. –
@ ŁukaszLech - aber ich habe es gesagt, dass es eine Liste und nach Gson Docs ist das der Weg, es zu tun ?! –
Tomer
Gibt Ihre Methode 'MapData' oder' List 'zurück? Ich vermute, dass Sie möglicherweise den falschen Typ zurückgeben. –