Learn more about com.google.gson library

Posted on by By admin, in Big Data | 0

In this article we are learning more about com.google.gson library .
Looking more about how to fetch data from JsonObject and JsonArray.

1. Using for loop

If we compare with net.sf.json library with Gson() to fetch JsonObject, there is small difference , we cannot put index value directly in getAsJsonObject() method, get() method should be use first.

Example:
Learn more about com.google.gson  library

If we don’t know what type of data is present in JsonObject such as,
String, JsonArray and JsonObject().

We can use get method only which returns JsonElement, from
JsonElement we can able to fetch different data.

JsonElement has method from which we can further fetch data according to our need

Under Standing JsonElement :

JsonElement accepts JsonObject, JsonArray and (String,int,boolean) as JsonPrimitive.
JsonElement in an interface.
Example:

Learn more about com.google.gson  library

2. Convert JsonObject to Map
First we have to convert jsonObject to string using toJson() method.
After that we will convert that into map using TypeToken() method.

Learn more about com.google.gson  library

We can now use this has Map.

Thank You
Amarnath Mali
Helical IT Solutions

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments