GSON parse generic Json Array

社会主义新天地 提交于 2019-12-11 19:19:13

问题


My question is very similiar to this: Unable to parse Json array using Gson

But I cann't get the answer from it. The answer from above link:

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

It works well, but I want to use implicit operator on generic type. See below:

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

And then I try to pass the Class argument to the method:

List<MapData> data = getData(MapData.class);
System.out.println(data.get(0).city);

Then an error was arised:

java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.ssc.ctq.nav.util.MapData 

Can anyone tell me why I get this error? Is implicit operator is not supported in TypeToken class?


回答1:


you can do like this:

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

    List<Map> tmpList = gson.fromJson(jsonString);
    List<T> resultList = new Arraylist<T>(tmplist.size());
    for(Map map:tmpList){
       String tmpJson = gson.toJson(map);
       resultList.add(gson.fromJson(tmpJson, classT));
    }
    return resultList;



回答2:


I met the same problem. From the Javadoc of TypeToken:

This syntax cannot be used to create type literals that have wildcard parameters, such as Class<?> or List<? extends CharSequence>.

You must explicitly indicate the type of T in TypeToken<T>, without generics.




回答3:


You can use this method in order to parse generic json string to map

    public Map<String, String> getMapFromJson(String jsonString) {
    Map<String, String> map = new HashMap<>();
    try {
        JSONObject object = new JSONObject(jsonString);
        Iterator<?> iterator = object.keys();
        while (iterator.hasNext()) {
            String key = (String) iterator.next();
            if(!key.isEmpty() && !object.getString(key).isEmpty()){
                map.put(key, object.getString(key));
            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return map;
}


来源:https://stackoverflow.com/questions/18712495/gson-parse-generic-json-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!