Unable to parse Json array using Gson

后端 未结 2 442
青春惊慌失措
青春惊慌失措 2020-12-10 15:27

I have this json array:

 [
{
\"id\":18,
\"city\":\"הרצליה\",
\"street\":\"החושלים 1\",
\"zipcode\":121209,
\"state\":\"IL\",
\"lat\":32.158138,
\"lng\":34.80         


        
2条回答
  •  我在风中等你
    2020-12-10 16:22

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

    Something like:

    public static List 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>(){}.getType();
        return gson.fromJson(jsonString, type);     
    }
    

    I have a fully working example of your issue in this Gist

提交回复
热议问题