简体   繁体   English

GSON解析通用Json数组

[英]GSON parse generic Json Array

My question is very similiar to this: Unable to parse Json array using Gson 我的问题与此非常相似: 无法使用Gson解析Json数组

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: 然后,我尝试将Class参数传递给方法:

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? TypeToken类不支持隐式运算​​符吗?

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;

I met the same problem. 我遇到了同样的问题。 From the Javadoc of TypeToken : TypeTokenJavadoc中

This syntax cannot be used to create type literals that have wildcard parameters, such as Class<?> or List<? extends CharSequence> 此语法不能用于创建具有通配符参数的类型文字,例如Class<?>List<? extends CharSequence> List<? extends CharSequence> . List<? extends CharSequence>

You must explicitly indicate the type of T in TypeToken<T> , without generics. 您必须在TypeToken<T>明确指出T的类型,而不能使用泛型。

You can use this method in order to parse generic json string to map 您可以使用此方法来解析要映射的通用json字符串

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM