简体   繁体   English

Gson和反序列化包含数组的对象数组

[英]Gson and deserializing an array of objects with arrays in it

I am trying to use Gson to deserialize a json string returned from my webservice 我试图使用Gson反序列化从我的webservice返回的json字符串

The structure would be returned as TypeDTO[] . 该结构将作为TypeDTO[]返回。

where TypeDTO is like TypeDTO就像

int id;
String name;
ArrayList<ItemDTO> items[] 

and ItemDTO is like 和ItemDTO就像

int id;
String name;
Boolean valid;

When I call the code as follows 当我调用代码如下

Gson gson = new Gson();
TypeDTO[] mytypes = (TypeDTO[]) gson.fromJson(reply, TypeDTO[].class);

Everything inside the objects is null 对象内的所有内容都为null

However, If I use the 但是,如果我使用的话

JSONArray and JSONObject to pull them out piece by piece from the org.json jars, it works fine and the fields are populated accordingly. JSONArrayJSONObject将它们从org.json jar中逐个拉出来,它工作正常并且字段相应地填充。

Any ideas as to what I'm doing wrong? 关于我做错了什么的任何想法? is Gson extremely fast? Gson非常快吗? Or am I better to stick with what I've got working already? 或者我最好坚持我已经工作的东西?

Thanks, David 谢谢,大卫

The example Java data structure in the original question does not match the description of the JSON structure in the comment. 原始问题中的示例Java数据结构与注释中的JSON结构的描述不匹配。

The JSON is described as JSON被描述为

"an array of {object with an array of {object}}". “一个{object with a array of {object}}”的数组。

In terms of the types described in the question, the JSON translated into a Java data structure that would match the JSON structure for easy deserialization with Gson is 根据问题中描述的类型,将JSON转换为与JSON结构匹配的Java数据结构,以便与Gson轻松反序列化,

"an array of {TypeDTO object with an array of {ItemDTO object}}". “一个{TypeDTO对象的数组,其数组为{ItemDTO object}}”。

But the Java data structure provided in the question is not this. 但问题中提供的Java数据结构不是这个。 Instead it's 相反,它是

"an array of {TypeDTO object with an array of an array of {ItemDTO object}}". “一个{TypeDTO对象的数组,其数组的数组为{ItemDTO object}}”。

A two-dimensional array != a single-dimensional array. 二维数组!=一维数组。

This first example demonstrates using Gson to simply deserialize and serialize a JSON structure that is "an array of {object with an array of {object}}". 第一个示例演示了使用Gson简单地反序列化和序列化JSON结构,该结构是“具有{object}}数组的对象的数组”。

input.json Contents: input.json内容:

[
  {
    "id":1,
    "name":"name1",
    "items":
    [
      {"id":2,"name":"name2","valid":true},
      {"id":3,"name":"name3","valid":false},
      {"id":4,"name":"name4","valid":true}
    ]
  },
  {
    "id":5,
    "name":"name5",
    "items":
    [
      {"id":6,"name":"name6","valid":true},
      {"id":7,"name":"name7","valid":false}
    ]
  },
  {
    "id":8,
    "name":"name8",
    "items":
    [
      {"id":9,"name":"name9","valid":true},
      {"id":10,"name":"name10","valid":false},
      {"id":11,"name":"name11","valid":false},
      {"id":12,"name":"name12","valid":true}
    ]
  }
]

Foo.java: Foo.java:

import java.io.FileReader;
import java.util.ArrayList;

import com.google.gson.Gson;

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

class TypeDTO
{
  int id;
  String name;
  ArrayList<ItemDTO> items;
}

class ItemDTO
{
  int id;
  String name;
  Boolean valid;
}

This second example uses instead a JSON structure that is actually "an array of {TypeDTO object with an array of an array of {ItemDTO object}}" to match the originally provided Java data structure. 第二个示例使用JSON结构,该结构实际​​上是“{NumberDTO对象的数组,其数组的数组为{ItemDTO object}}”以匹配最初提供的Java数据结构。

input.json Contents: input.json内容:

[
  {
    "id":1,
    "name":"name1",
    "items":
    [
      [
        {"id":2,"name":"name2","valid":true},
        {"id":3,"name":"name3","valid":false}
      ],
      [
        {"id":4,"name":"name4","valid":true}
      ]
    ]
  },
  {
    "id":5,
    "name":"name5",
    "items":
    [
      [
        {"id":6,"name":"name6","valid":true}
      ],
      [
        {"id":7,"name":"name7","valid":false}
      ]
    ]
  },
  {
    "id":8,
    "name":"name8",
    "items":
    [
      [
        {"id":9,"name":"name9","valid":true},
        {"id":10,"name":"name10","valid":false}
      ],
      [
        {"id":11,"name":"name11","valid":false},
        {"id":12,"name":"name12","valid":true}
      ]
    ]
  }
]

Foo.java: Foo.java:

import java.io.FileReader;
import java.util.ArrayList;

import com.google.gson.Gson;

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

class TypeDTO
{
  int id;
  String name;
  ArrayList<ItemDTO> items[];
}

class ItemDTO
{
  int id;
  String name;
  Boolean valid;
}

Regarding the remaining two questions: 关于其余两个问题:

is Gson extremely fast? Gson非常快吗?

Not compared to other deserialization/serialization APIs. 与其他反序列化/序列化API不相比。 Gson has traditionally been amongst the slowest . 传统上 Gson是最慢的 The current and next releases of Gson reportedly include significant performance improvements, though I haven't looked for the latest performance test data to support those claims. 据报道,Gson的当前和下一版本包括显着的性能改进,但我没有寻找最新的性能测试数据来支持这些声明。

That said, if Gson is fast enough for your needs, then since it makes JSON deserialization so easy, it probably makes sense to use it. 也就是说,如果Gson足够快以满足您的需求,那么因为它使JSON反序列化变得如此简单,所以使用它可能是有意义的。 If better performance is required, then Jackson might be a better choice to use. 如果需要更好的性能,那么杰克逊可能是更好的选择。 It offers much (maybe even all) of the conveniences of Gson. 它提供了很多(甚至可能是所有)Gson的便利。

Or am I better to stick with what I've got working already? 或者我最好坚持我已经工作的东西?

I wouldn't. 我不会。 I would most always rather have one simple line of code like 我总是宁愿有一行简单的代码

TypeDTO[] myTypes = gson.fromJson(new FileReader("input.json"), TypeDTO[].class);

...to easily deserialize into a complex data structure, than the thirty lines of code that would otherwise be needed to map the pieces together one component at a time. ...轻松地反序列化为复杂的数据结构,而不是将一个组件一次映射到一个组件所需的三十行代码。

Use your bean class like this, if your JSON data starts with an an array object. 如果您的JSON数据以数组对象开头,请使用这样的bean类。 it helps you. 它可以帮助你。

Users[] bean = gson.fromJson(response,Users[].class);

Users is my bean class. 用户是我的bean类。

Response is my JSON data. 响应是我的JSON数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM