创建动态类型列表 [英] Creating lists of dynamic types

查看:189
本文介绍了创建动态类型列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我的工作,现在就试图找出如何分析大量不同的JSON对象,而无需硬code函数序列化和反序列化的每个对象。我开始使用GSON,并一直致力于pretty好。一个问题开始出现,虽然当我尝试使用对象的列表。

So, I'm working right now on trying to figure out how to parse a large number of different JSON objects without having to hard-code functions to serialize and deserialize each of the objects. I started using Gson, and that has been working pretty well. A problem begins to appear though when I am trying to use a list of objects.

我想创建,将与我的任何数据模型对象的工作,我可以通过一个JSONArray,而且JSON包含类类型的函数,这样我就需要打个电话,看起来是这样的 -

I would like to create a function that will work with any of my data model objects that I can pass a JSONArray and the class type that the JSON contains, so that I would have a call that looks something like this -

response = JSONHelper.createObjectList(jsonArray, DataObject.class);

顺便说一句,我不是从我的dev的机器发布,所以请原谅我,如果有我的code几个错别字。

我试图写这样的事情(我JSONHelper类中)来创建效果 -

I tried to create that effect by writing something like this (inside my JSONHelper class) -

public List createObjectList(JSONArray jsonArray, Class type) {
    Gson gson = new Gson();

    Type listType = new TypeToken<List<type>>() {}.getType();

    List<type> data = gson.fromJson(jsonArray, listType);

    return data;
}

好吧,显然不工作code,但就是有点我怎么想能够得到这个工作。我尝试了几种不同的方式和变化,但我一直没能找出什么,真的很好的作品。

Okay, so obviously that isn't working code, but that is kinda how I wanted to be able to get this to work. I tried several different ways and variations, but I haven't been able to figure out anything that works really nicely.

我是新来了Android / Java的世界,所以我还在学习中的和语言的出来的。如果有人能给我我怎么能做到什么,我找一些小技巧,那将是非常有益的。谢谢你。

I am new to the Android/Java world, so i'm still learning the in's and out's of the language. If someone could give me some tips on how I could achieve what I am looking for, it would be very helpful. Thanks.

推荐答案

答案是:你不能做你想要做在Java中,因为类型擦除什么。

The answer is: You can't do what you're trying to do in Java because of type erasure.

这就是的原因的,你必须使用 TypeToken 并传递一个键入来GSON的 fromJson()方法;为 T 类型的信息是不是在运行时可用。在Java泛型是在大多数情况下只是编译时的类型检查。

That's the reason you have to use TypeToken and pass a Type to Gson's fromJson() method; the type information for T isn't available at runtime. Generics in Java are for the most part just compile time type checking.

在理想情况下,你想成为能够做到这一点:

Ideally you'd like to be able to do this:

public <T> List<T> createList(JsonArray a, Class<T> clazz)
{
    Type t = new TypeToken<List<T>>(){}.getType();
    return new Gson().fromJson(a, t);
}

但你不能,因为ŧ被擦除。如果你试试这个,你会发现结果完全乱了套;你收到了列表回来了,但它不会的真正的包含您的类并抛出一个转换异常。

But you can't, because T gets erased. If you try this you'll find the result completely confusing; you get a List back, but it won't actually contain your class and throw a cast exception.

实际上,你的方法就必须是这样的:

Effectively, your method would have to look like this:

public <T> List<T> createList(JsonArray a, Type t)
{
    List<T> list = new Gson().fromJson(a, t);
    return list;
}

当然,这将是愚蠢的,因为你现在打电话给你写的,而不是只调用GSON方法的方法。

Which of course would be silly since you're now calling a method you wrote instead of just calling the method in Gson.

编辑补充:

关于它的思考,有办法解决这个为列表类型。您可以使用数组:

Thinking about it, there's way around this for List types. You can use arrays:

public <T> List<T> createList(JsonArray a, Class<T[]> c)
{
    T[] array = new Gson().fromJson(a, c);
    return Arrays.asList(array);
}

由于你不是试图依靠泛型类型从的的一个列表,而是传入的类数组类型。 .. 有用。有点乱,但它的作品:

Because you're not trying to rely on the generic type from inside a List and instead are passing an array type for the class... it works. A bit messy, but it works:

List<MyPojo> list = createList(a, MyPojo[].class);

这篇关于创建动态类型列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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