Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use Google's Gson API to deserialize JSON properly?

Tags:

In short, this is a sketch of the JSON object I want to parse in JAVA:

{     object1: {             item1: //[String | Array | Object] ,             item2: // ...             //<> more items     object2: { /* .. */ }     //<> more objects } 

These are the POJO s I created for parsing (I'll leave out the import statements for brevity's sake):

(1) The representation of the complete JSON object

public class JObjectContainer {      private List<JObject> jObjects ;      public JObjectContainer() { }      //get & set methods  } 

(2) The representation of the nested objects:

 public class JObject {      private String id ;     private List<JNode> jObjects ;      public JObject() { }       //get & set methods  } 

(3) The representation of the items:

 public class JNode {      private JsonElement item1 ;     private JsonElement item2 ;     //<> more item fields      public JNode() { }      //get & set methods  } 

Now, creating a Gson instance (FileReader for importing the jsonFile),

 Gson gson = new Gson() ;  JObjectContainer joc = gson.fromJson(jsonFile,JObjectContainer.class) ; 

I get a NullPointerException whenever I try to access the parseable object (e.g. through a ListIterator). Gson does however create an object of the class I specified and does not throw any subsequent errors.

I know that this has been done before. So, what am I missing?

TIA

like image 578
FK82 Avatar asked May 19 '10 09:05

FK82


People also ask

How do I deserialize JSON with Gson?

Deserialization – Read JSON using Gson. Deserialization in the context of Gson means converting a JSON string to an equivalent Java object. In order to do the deserialization, we need a Gson object and call the function fromJson() and pass two parameters i.e. JSON string and expected java type after parsing is finished ...

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.

What does Gson to JSON do?

Gson (by Google) is a Java library that can be used to convert a Java object into JSON string. Also, it can used to convert the JSON string into equivalent java object.


1 Answers

That's not possible. You need to modify your JSON structure to represent object1, object2, etc as items of an array. Right now they are properties of an object of which it's apparently unknown how many of them will be (else you didn't attempt to map it as a List). Gson is smart, but not that smart :)

So, as a basic example, this JSON structure with an array:

{ nodes:   [     { item1: 'value1a', item2: 'value2a' },     { item1: 'value1b', item2: 'value2b' },     { item1: 'value1c', item2: 'value2c' }   ] } 

in combination with the Java representation (which is not necessarily to be called POJO, but just javabean or model object or value object).

public class Container {     private List<Node> nodes;     // +getter. }  public class Node {     private String item1;     private String item2;     // +getters. } 

and this Gson call

Container container = new Gson().fromJson(json, Container.class); 

should work.

Update: to be clear, your JSON structure is the problem, not your Java object structure. Assuming that your Java object structure is exactly what you would like to end up with, then your JSON structure should look like follows to get Gson to do its job:

{ jObjects:   [     { id: 123, jObjects:        [         { item1: 'value1a', item2: 'value2a' },         { item1: 'value1b', item2: 'value2b' },         { item1: 'value1c', item2: 'value2c' }         /* etc... commaseparated */       ]     },     { id: 456, jObjects:        [         { item1: 'value1d', item2: 'value2d' },         { item1: 'value1e', item2: 'value2e' },         { item1: 'value1f', item2: 'value2f' }         /* etc... commaseparated */       ]     }     /* etc... commaseparated */   ] } 

Only the JsonElement property should be replaced by String, since it's invalid.

like image 99
BalusC Avatar answered Oct 05 '22 10:10

BalusC