Unable to parse Json array using Gson

后端 未结 2 440
青春惊慌失措
青春惊慌失措 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<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

    0 讨论(0)
  • 2020-12-10 16:25
    string Json="some Json String";
    JavaScriptSerializer Js = new  JavaScriptSerializer();
    MapData ObjMapDat = new MapData ();
    ObjMapDat =Js.Deserialize<MapData>(Json);
    
    0 讨论(0)
提交回复
热议问题