Gson和反序列化其中包含数组的对象数组 [英] Gson and deserializing an array of objects with arrays in it

查看:103
本文介绍了Gson和反序列化其中包含数组的对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Gson反序列化从我的webservice返回的json字符串



该结构将返回为 TypeDTO []

code>。



其中 TypeDTO 就像

  int id; 
字符串名称;
ArrayList< ItemDTO> items []

和ItemDTO就像

  int id; 
字符串名称;
布尔型有效;

当我按以下方式调用代码时:

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

对象内的所有内容都为空



但是,如果我使用

JSONArray JSONObject 把它们从org.json瓶子里一块一块拉出来,它工作正常,并且相应的字段被填充。



任何关于我在做什么的想法都是错误的? Gson非常快?
还是我最好坚持我已经工作过的?

感谢,
David

解决方案

原始问题中的示例Java数据结构与评论中JSON结构的描述不匹配。



JSON被描述为

{object with array of {object}}的数组。



根据问题中描述的类型,将JSON转换为与JSON结构相匹配的Java数据结构,以便于与Gson进行简单的反序列化操作。

一个{TypeDTO对象的数组{数组} {ItemDTO对象}}。



但问题中提供的Java数据结构不是这样的。相反,它是 $ b

一个{TypeDTO对象的数组,其数组由{ItemDTO对象}}。

一个二维数组!!=一维数组。

这个第一个例子演示了如何使用Gson简单地对一个JSON结构进行反序列化和序列化, {对象与{对象}}的数组。



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 名称9,valid:true},
{id:10,name:name10,valid:false},
{id:11, :name11,valid:false},
{id:12,name:name12,valid:true}
]
}
]

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;
字符串名称;
ArrayList< ItemDTO>项目;
}

class ItemDTO
{
int id;
字符串名称;
布尔型有效;
}

第二个例子使用了一个JSON结构,它实际上是一个{ TypeDTO对象,其中包含一个{ItemDTO对象}}数组的数组}以匹配最初提供的Java数据结构。



input.json内容:




$ name $b 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:

  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;
字符串名称;
ArrayList< ItemDTO>项[];
}

class ItemDTO
{
int id;
字符串名称;
布尔型有效;
}

关于剩余的两个问题:


是Gson非常快吗?

未与其他反序列化/序列化API进行比较。 Gson传统上是 之间的 slowest 。据报道,Gson的当前版本和下一版本包括显着的性能改进,尽管我没有寻找最新的性能测试数据来支持这些索赔。

也就是说,如果Gson对于你的需求足够快,那么因为它使JSON反序列化变得如此简单,所以使用它可能是有意义的。如果需要更好的性能,那么Jackson可能是更好的选择。它提供了很多(甚至可能是所有)Gson的便利。


还是我最好坚持我已经工作的?


我不会。我总是宁愿有一行简单的代码,比如

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

...可以轻松地反序列化为一个复杂的数据结构,而不是三十行代码需要一次将一个组件映射到一起。

I am trying to use Gson to deserialize a json string returned from my webservice

The structure would be returned as TypeDTO[].

where TypeDTO is like

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

and ItemDTO is like

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

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.

Any ideas as to what I'm doing wrong? is Gson extremely fast? 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.

The JSON is described as

"an array of {object with an 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

"an array of {TypeDTO object with an array of {ItemDTO object}}".

But the Java data structure provided in the question is not this. Instead it's

"an array of {TypeDTO object with an array of an array of {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}}".

input.json Contents:

[
  {
    "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:

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.

input.json Contents:

[
  {
    "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:

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?

Not compared to other deserialization/serialization APIs. Gson has traditionally been amongst the slowest. 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.

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. 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.

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.

这篇关于Gson和反序列化其中包含数组的对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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