使用Gson对通用集合进行反序列化 [英] Deserialization of generic collections with Gson

查看:92
本文介绍了使用Gson对通用集合进行反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用GSon进行json反序列化时遇到了一些困难,希望有人能帮助我.

I have some difficulties with json deserialization using GSon and I hope somebody can help me.

我想反序列化以下json片段:

I want to deserialize the following json snippet:

{
    "fieldA": "valueA",
    "myCollection": {
        "AnotherClass": [
            {
                "objectAfieldA": "valueB",
                "objectAfieldB": "valueC"
            },
            {
                "objectAfieldA": "valueD",
                "objectAfieldB": "valueE"
            }
        ]
    }
}

相应的整体类具有以下字段:

the corresponding overall class has following fields:

...
String fieldA;
List<AnotherClass> = new ArrayList<AnotherClass>();
....

现在,我的问题是,当我使用fromJson(jsonSample, resultContainer.class)反序列化时,如果没有List<T>元素,一切都很好,但是当包含包含的列表时,我得到的是NullPointerException.我已经读过有关如何处理泛型类型的集合以及TypeToken的用法的信息,但是当我的集合是另一个类的一部分时,我将无法应用此知识……

Now, my problem is that when I deserialize, using fromJson(jsonSample, resultContainer.class), without the List<T> element, everything is good, but I get a NullPointerException when I include the contained list. I've read about how to deal with collections of generic types and the use of TypeToken, but I can't apply this knowledge when my collection is part of another class…

我真的很感激能解决这个问题.

I really would appreciate any help to solve this.

推荐答案

反序列化时,如果要反序列化的最外层结构是通用集合,则只需使用TypeToken.原始问题中的示例不是这种情况.因此,不需要使用TypeToken.

When deserializing, you only need to use the TypeToken if the outer-most structure to be deserialized into is a generic collection. This is not the case for the example in the original question. So, use of a TypeToken is unnecessary.

问题似乎是JSON结构与尝试绑定的Java结构不匹配.

The issue appears to be that the JSON structure does not match the Java structure attempting to be bound to.

JSON结构定义

an object with two elements
    element 1 is a string named "fieldA",
    element 2 is an object named "myCollection", which has one element
        the one element is an array named "AnotherClass", composed of objects with two elements
            element 1 is a string named "objectAfieldA",
            element 2 is a string named "objectAfieldB"

因此,定义一个Java数据结构来匹配它,反序列化将非常简单地进行,而无需任何自定义处理.如果未提供这种匹配的Java结构,则必须进行自定义反序列化.

So, define a Java data structure to match that, and deserialization will work very simply, without any custom processing necessary. If such a matching Java structure is not provided, then custom deserialization is necessary.

这是一个使用原始问题的名称和类型的工作示例.

Here is such a working example using the names and types from the original question.

import java.io.FileReader;

import com.google.gson.Gson;

public class Foo
{
  public static void main(String[] args) throws Exception
  {
    Gson gson = new Gson();
    resultContainer result = gson.fromJson(new FileReader("input.json"), resultContainer.class);
    System.out.println(gson.toJson(result));
  }
}

class resultContainer
{
  String fieldA;
  MyCollectionContainer myCollection;
}

class MyCollectionContainer
{
  SomeOtherClass[] AnotherClass;
}

class SomeOtherClass
{
  String objectAfieldA;
  String objectAfieldB;
}

这篇关于使用Gson对通用集合进行反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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