与反序列化或GSON嵌套内部类中的内部类任意对象的JSON数组 [英] Deserializing arbitrary object json arrays in inner classes with Gson or nested inner class

查看:1211
本文介绍了与反序列化或GSON嵌套内部类中的内部类任意对象的JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有麻烦时,我尝试反序列化1 JSON字符串与GSON。该字符串是这样的
(注:我只是简化了,但留给我有正因为如此,有可能是Json的构造结错误烦恼的一部分,但我已经有一个在线验证,我一起工作的字符串即可选中):

I am having troubles when I try to deserialize one Json string with Gson. The string goes something like this (Note: I just simplified it, but leaving the part I am having troubles with and because of that, there might be Json syntaxis errors, but I have checked with an online validator that the string I am working with is OK):

// let's call this "container" json element
{
"context": "context", 
"cpuUsage": cpuUsageValue,  
"name": "thename",
"rates": {
    "definition": [
        {
        "key": "name",
        "type": "string"
        },
        {
        "key": "rate",
        "type": "double"
        }       
    ]
    "rows": [
        {
        "name": "thename1",
        "rate": therate
        },
        {
        "name": "thename2",
        "rate": therate2
        }
    ]
}

现在,我得到的问题是,当我尝试反序列化JSON阵列(定义和行)。场休息得到反序列化正确的价值观。
我使用的类定义如下(不干将/为简单的setter):

Now, the problem I get is when I try to deserialize the json arrays ("definition" and "rows"). Rest of fields get proper values in deserialization. The class definition I am using is the following (no getters/setters for simplicity):

public class Container
{
   private String context;
   private Double cpuUsage;
   private String name;   
   private RateContainer rates;

   public Container()
   {

   }
}

RateContainer(内部静态类类容器,根据GSON规格):

RateContainer (inner static class to class Container, according to Gson specs):

public static class RateContainer
{
    private List<DefinitionContainer> definition;
    private List<RowsContainer> rows;

    public static class DefinitionContainer
    {
        String key;
        String type;

        public DefinitionContainer()
        {
        }
    }

    public static class RowsContainer
    {
        String name;
        Double rate; 

        public RowsContainer()
        {
        }
    }

    public RateContainer()
    {
    }
}

要解析JSON字符串,我使用:

To parse the Json string, I use:

Container container = gson.fromJson(containerString, Container.class);

和我得到了以下异常:

Expecting object found: [{"key":"name","type":"string"},{"key":"rate","type":"double"}]

看起来必须有东西在类的定义,不很好地工作。我检查了GSON API,我知道,为了反序列化列表,通常要做的事情是:

Looks like there has to be something in the class definition that does not work well. I have checked the Gson API and I know that, in order to deserialize lists, the usual thing to do is:

Type collectionType = new TypeToken<Collection<Integer>>(){}.getType();
Collection<Integer> ints2 = gson.fromJson(json, collectionType);

所以我想,也许我能先取得这些阵列,使用类似:

so I thought that maybe I could get these arrays first, using something like:

JsonElement element = containerJsonElement.getAsJsonObject().get("rates");

然后得到定义和行,但我想preFER把一切都在容器对象。
有没有一种方法来反序列化以这样的方式这些名单?
有什么不对的类定义?

and then get "definition" and "rows", but I would prefer to keep everything in the Container object. Is there a way to deserialize these lists in such a way? Is there something wrong in the class definition?

感谢大家提前!

推荐答案

在回应一些东西,在原来的问题,注意以下三点:

In response to a few things in the original question, note the following three things:


  1. GSON不需要反序列化到静态内部类。

  2. 这是唯一需要使用泛型 TypeToken 如果要反序列化的类型是一个泛型集合。如果类型进行反​​序列化的唯一的包含的这样一个集合,然后用一个泛型 TypeToken 是没有必要的。

  3. 使用类似GSON或杰克逊的API的一个主要好处是从JSON简单的映射和序列化/反序列化Java数据结构来/。因此,像 JsonElement explicite使用的组件是可以避免的。

  1. Gson does not require deserialization to static inner classes.
  2. It's only necessary to use a generified TypeToken if the type to be deserialized to is a generified collection. If the type to be deserialized to only contains such a collection, then use of a generified TypeToken is not necessary.
  3. A major benefit of using an API like Gson or Jackson is for simple mapping and serialization/deserialization of Java data structures to/from JSON. So, explicite use of components like JsonElement can be avoided.

通过修正为

{
    "context": "context", 
    "cpuUsage": "cpuUsageValue",  
    "name": "thename",
    "rates": {
        "definition": [
            {
                "key": "name",
                "type": "string"
            },
            {
                "key": "rate",
                "type": "double"
            }       
        ],
        "rows": [
            {
                "name": "thename1",
                "rate": "therate"
            },
            {
                "name": "thename2",
                "rate": "therate2"
            }
        ]
    }
}

...那么下面的反序列化(和序列化),简单,符合市场预期。

...then the following deserializes (and serializes) simply and as expected.

import java.io.FileReader;
import java.util.List;

import com.google.gson.Gson;

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

class Container
{
  private String context;
  private String cpuUsage;
  private String name;
  private Rates rates;
}

class Rates
{
  private List<Definition> definition;
  private List<Row> rows;
}

class Definition
{
  private String key;
  private String type;
}

class Row
{
  private String name;
  private String rate;
}

这篇关于与反序列化或GSON嵌套内部类中的内部类任意对象的JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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