8

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;
    }

}   

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?

5
  • 2
    Type erasure. Gson knows that he has to deserialize java.util.List, but has no idea, what objects should he put into it, so he uses internal types. Commented Jul 15, 2013 at 9:12
  • @ŁukaszLech - but i told it that its a List<MapData> and according to Gson docs that is the way to do it?! Commented Jul 15, 2013 at 9:15
  • Does your method return MapData or List<MapData>? I suspect you may be returning the wrong type. Commented Jul 15, 2013 at 9:17
  • @KevinBowersox - You have a point there, it is actually a generic method (simplified for this example), and i pass in a single object type, not a list type Commented Jul 15, 2013 at 9:22
  • @ftom2 See the answer I just posted. It contains a GIST with a working example. I did strip out the different charset, but I don't think that would cause an issue. Commented Jul 15, 2013 at 9:28

2 Answers 2

32

I suspect that the method calling fromJson is returning the wrong type. It should return a 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 a fully working example of your issue in this Gist

Sign up to request clarification or add additional context in comments.

1 Comment

After searching for 2 days, finally got the correct approach to solve. Thanks a lot Kevin!
-1
string Json="some Json String";
JavaScriptSerializer Js = new  JavaScriptSerializer();
MapData ObjMapDat = new MapData ();
ObjMapDat =Js.Deserialize<MapData>(Json);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.