Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gson and deserializing an array of objects with arrays in it

Tags:

java

json

gson

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

like image 550
DavieDave Avatar asked Sep 21 '10 19:09

DavieDave


People also ask

How do you parse an array of objects in Java?

//Parsing the contents of the JSON file JSONObject jsonObject = (JSONObject) jsonParser. parse(new FileReader("E:/players_data. json")); Retrieve the value associated with a key using the get() method.

What is Gson serialization and Deserialization?

Gson can serialize a collection of arbitrary objects but can't deserialize the data without additional information. That's because there's no way for the user to indicate the type of the resulting object. Instead, while deserializing, the Collection must be of a specific, generic type.

Is Gson better than Jackson?

ConclusionBoth Gson and Jackson are good options for serializing/deserializing JSON data, simple to use and well documented. Advantages of Gson: Simplicity of toJson/fromJson in the simple cases. For deserialization, do not need access to the Java entities.

What does Gson toJson do?

Introduction. Gson is the main actor class of Google Gson library. It provides functionalities to convert Java objects to matching JSON constructs and vice versa. Gson is first constructed using GsonBuilder and then toJson(Object) or fromJson(String, Class) methods are used to read/write JSON constructs.


2 Answers

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.

like image 138
Programmer Bruce Avatar answered Oct 20 '22 03:10

Programmer Bruce


Use your bean class like this, if your JSON data starts with an an array object. it helps you.

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

Users is my bean class.

Response is my JSON data.

like image 22
nagabrahmam Avatar answered Oct 20 '22 03:10

nagabrahmam