2

I'm getting following JSON response: "data" in object:

"status": true,
"data":{
    //Some data
}

Sometimes it comes in array as:

"status": true,
"data":[
    //Some data
]

How to check data's response dynamically that, is object or array? *I'm using Retrofit

My Retrofit parsing for array as:

@SerializedName("data")
ArrayList<DataDetail> dataList;

Thanks in advance!

Pratik Pitale
  • 1,655
  • 1
  • 16
  • 30
  • possible duplicate of [Determine whether JSON is a JSONObject or JSONArray](http://stackoverflow.com/questions/6118708/determine-whether-json-is-a-jsonobject-or-jsonarray). – Akash Patel Jan 11 '17 at 06:32
  • I think you are not able to manage this all because everything is happening related to json parsing in background for your retrofit. So where you will check for instanceof. You have to change your retrofit structure. – Ready Android Jan 11 '17 at 06:38
  • Yes, i'm facing exactly same problem. @Ready Android – Pratik Pitale Jan 11 '17 at 06:45
  • Kindly check updated question @Ready Android – Pratik Pitale Jan 11 '17 at 06:47

2 Answers2

4

If the Json is unknown that whether it will be JsonObject or JsonArray, then simply use JsonElement like below:

@SerializedName("data")
private JsonElement data;

Now to convert this JsonElement to your respective model as per your requirement you can use below code:

if(data instanceOf JsonObject){
    YourModelForData object = YourDataComponentForObject(data);
    // Do anything with Object
} else {
    List<YourModelForData> array = YourDataComponentForArray(data);
    // Do anything with array
}

public YourModelForData YourDataComponentForObject(JsonElement data) {
     Type type = new TypeToken<YourModelForData>() {
     }.getType();
     YourModelForData item = new Gson().fromJson(data, type);
}

public List<YourModelForData> YourDataComponentForArray(JsonElement data) {
      Type type = new TypeToken<List<YourModelForData>>() {
      }.getType();
      List<YourModelForData> items = new Gson().fromJson(data, type);
}

Happy Coding <{}>;

Siraj Sumra
  • 934
  • 1
  • 11
  • 28
  • this method is returning only list but what about when only object comes ? – Pratik Pitale Jan 11 '17 at 07:39
  • I have edited my answer, Please try that. Because check of object or array you have to maintain using instanceof and let the JsonElement handle the serialise datatype. You can also typecast json using getAsJsonObject or getAsJsonArray using gson. – Siraj Sumra Jan 11 '17 at 08:23
  • Look good !!, but where to call above method, and what it will return ? @Siraj Sumra – Pratik Pitale Jan 11 '17 at 10:12
  • Edited the answer. Use it that way. Try to use the object and array according to your requirement as per edited answer. Hope it help – Siraj Sumra Jan 11 '17 at 10:48
  • Object parsing worked but having some problem with array, Error getting as: Expected BEGIN_OBJECT but was BEGIN_ARRAY – Pratik Pitale Jan 11 '17 at 14:08
  • You can typecast the JsonElement with getAsJsonArray. Try using it. Also try to give condition as else if(data instanceof JsonArray). Hope it works – Siraj Sumra Jan 12 '17 at 06:13
2
JSONObject json;
Object     intervention;
JSONArray  interventionJsonArray;
JSONObject interventionObject;

json = RestManager.getJSONfromURL(myuri); // retrieve the entire json stream     
Object intervention = json.get("intervention");
if (intervention instanceof JSONArray) {
    // It's an array
    interventionJsonArray = (JSONArray)intervention;
}
else if (intervention instanceof JSONObject) {
    // It's an object
    interventionObject = (JSONObject)intervention;
}
else {
    // It's something else, like a string or number
}

check this thread Test if it is JSONObject or JSONArray

Community
  • 1
  • 1