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:
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:
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.
We can now use this has Map.
Thank You
Amarnath Mali
Helical IT Solutions