Dear Roma,
Hope you are doing well.
We sincerely apologize for the delay.
Your inner object can be a json object or a json array, so It will use a generic JsonElement of gson
public class CustomJson {
private int type;
private JsonElement object;
}
Then in your Activity, using Gson to parse:
Gson gson = new Gson();
CustomJson[] customJson = gson.fromJson(jsonString, CustomJson[].class);
Because com.google.gson.JsonElement can be a JsonObject (not JSONObject) or a JsonArray (not JSONArray), if you want to convert to JSONObject and JSONArray, use the following code:
if (customJson!= null && customJson.length > 0) {
for (CustomJson aCustomJson : customJson) {
JsonElement jsonElement = aCustomJson.object;
if (jsonElement instanceof JsonObject) {
try {
JSONObject jsonObject = new JSONObject(jsonElement.toString());
Log.i(LOG_TAG, jsonObject.toString());
} catch (JSONException e) {
e.printStackTrace();
}
} else if (jsonElement instanceof JsonArray) {
try {
JSONArray jsonArray = new JSONArray(jsonElement.toString());
Log.i(LOG_TAG, jsonArray.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
public class CustomJson {
private int type;
private JsonElement object;
}
Then in your Activity, using Gson to parse:
Gson gson = new Gson();
CustomJson[] customJson = gson.fromJson(jsonString, CustomJson[].class);
Because com.google.gson.JsonElement can be a JsonObject (not JSONObject) or a JsonArray (not JSONArray), if you want to convert to JSONObject and JSONArray, use the following code:
if (customJson!= null && customJson.length > 0) {
for (CustomJson aCustomJson : customJson) {
JsonElement jsonElement = aCustomJson.object;
if (jsonElement instanceof JsonObject) {
try {
JSONObject jsonObject = new JSONObject(jsonElement.toString());
Log.i(LOG_TAG, jsonObject.toString());
} catch (JSONException e) {
e.printStackTrace();
}
} else if (jsonElement instanceof JsonArray) {
try {
JSONArray jsonArray = new JSONArray(jsonElement.toString());
Log.i(LOG_TAG, jsonArray.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
I think this will resolve the query.
Please let me know if you have any further issue regarding this.
Feel free to contact us in case you have any query.