android - JSON Java check element is a JSONArray or JSONObject -


How to check how an element is a JSONArray or JSONObject I have written to check the code,

  if (jsonObject.getJSONObject ("category"). GetClass (). IsArray ()) {} else {}   

In this case if the element 'category' JSONObject So it works fine, but if it has an array it throws an exception: JSONArray can not be converted to JSONObject. Please help. Thank you.

Yes, this is because getJSONObject ("category") which Trying to convert string to JSONObject, which will throw JSONException , you should do the following:

Check that using an object JSONObject is:

  JSONObject category = jsonObject.optJSONObject ("category");   

will return JSONObject or null if the category object is not a Jason object. Then you do the following:

  JSONArray categories; If (category == zero) categories = jsonObject.optJSONArray ("category");   

will return your JSONArray or tap if it is not valid JSONArray .

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -