将json字符串反序列化为一般类型列表 [英] Deserialize json string into generically typed list

查看:128
本文介绍了将json字符串反序列化为一般类型列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下是我第一次尝试。

  public abstract class AbstractService< T> {

public abstract Class< T> getClazz();

public List< T> deserialize(final String json){
Gson gson = gsonFactory.create();
Type listType = new TypeToken< List< T>>(){} .getType();
列表< T> entityList = gson.fromJson(json,listType);
返回entityList;


然而,由于类型擦除,T in: new TypeToken< List< T>>(){} .getType(); 在运行时不可用。因此,Gson不会返回我的实体列表,而是返回Gson Map对象的列表。



注意,我确实可以在运行时访问具体类T通过调用getClazz()。虽然我不确定我可以如何使用它来指示Gson发送给我一个特定类型的列表。



有没有人知道如何让它工作?



任何帮助将不胜感激。

解决方案

解决了问题。

根据gson文档这里,你需要将字符串拆分为单独的元素,并将这些元素反序列化。



使用Gson的解析器API(低级流解析器或DOM解析器JsonParser)来解析数组元素,然后在每个数组元素上使用Gson.fromJson(),这是首选方法。是

所以我的解决方案变成:

  public abstract class AbstractService< T> {

public abstract Class< T> getClazz();

public List< T> deserialize(final String json){
JsonArray array = parser.parse(json).getAsJsonArray();
最终列表< T> entityList = new ArrayList< V>();
for(final JsonElement jsonElement:array){
T entity = gson.fromJson(jsonElement,getClazz());
entityList.add(entity);
}
返回entityList;
}
}


I want to include in my Service objects a generic way of deserializing lists of objects from a json string.

Below was my first attempt.

public abstract class AbstractService<T>{

    public abstract Class<T> getClazz();

    public List<T> deserialize(final String json){
        Gson gson = gsonFactory.create();
        Type listType = new TypeToken<List<T>>() {}.getType();
        List<T> entityList = gson.fromJson(json, listType);
        return entityList;
    }
}

However due to type erasure, the T in: new TypeToken<List<T>>() {}.getType(); is not available at run time. So instead of getting a list of my entities back, Gson returns a list of Gson Map objects.

NOTE, that I do have access at runtime to the concrete class of T, by calling getClazz(). Although I'm not sure how I can use this to instruct Gson to send me back a list of a certain type.

Does anyone know a way of getting this to work?

Any help would be appreciated.

解决方案

Worked it out.

According to gson docs here, you need to split the string into individual elements, and deserialize those seperately.

"Use Gson's parser API (low-level streaming parser or the DOM parser JsonParser) to parse the array elements and then use Gson.fromJson() on each of the array elements. This is the preferred approach. Here is an example that demonstrates how to do this."

So my solution becomes:

public abstract class AbstractService<T>{

    public abstract Class<T> getClazz();

    public List<T> deserialize(final String json){
        JsonArray array = parser.parse(json).getAsJsonArray();
        final List<T> entityList = new ArrayList<V>();
        for(final JsonElement jsonElement: array){
            T entity = gson.fromJson(jsonElement, getClazz());
            entityList.add(entity);
        }
        return entityList;
    }
}

这篇关于将json字符串反序列化为一般类型列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆