1

当我尝试使用 Gson 反序列化一个 Json 字符串时遇到了麻烦。字符串是这样的(注意:我只是简化了它,但留下了我遇到问题的部分,因此,可能存在 Json 语法错误,但我已经使用在线验证器检查了我正在使用的字符串没问题):

// 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 数组(“定义”和“行”)时。其余字段在反序列化中获得适当的值。我正在使用的类定义如下(为简单起见,没有 getter/setter):

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

   public Container()
   {

   }
}

RateContainer(根据 Gson 规范,内部静态类到类 Container):

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 字符串,我使用:

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

我得到以下异常:

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

看起来类定义中必须有一些不能正常工作的东西。我检查了 Gson API,我知道,为了反序列化列表,通常要做的是:

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

所以我想也许我可以先得到这些数组,使用类似的东西:

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

然后获取“定义”和“行”,但我更愿意将所有内容保留在 Container 对象中。有没有办法以这种方式反序列化这些列表?类定义有问题吗?

谢谢大家!

4

1 回答 1

6

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

  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.

With the example JSON corrected as

{
    "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;
}
于 2011-06-09T16:57:28.443 回答