I am getting this error when I am trying to convert following JSON response string from the server. I want to process JSONObject or JSONArray depending on the response from the server as most of the time it returns JSONArray.
JSON response from server
jsonString = {"message":"No Results found!","status":"false"}
Java code is as below
try
{
JSONArray jsonArrayResponse = new JSONArray(jsonString);
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT)
{
if(jsonArrayResponse != null && jsonArrayResponse.length() > 0)
{
getCancelPurchase(jsonArrayResponse.toString());
}
}
}
catch(JSONException e)
{
e.printStackTrace();
}
Error Log:
org.json.JSONException: Value {"message":"No Results found!","status":"false"} of type org.json.JSONObject cannot be converted to JSONArray
at org.json.JSON.typeMismatch(JSON.java:111)
at org.json.JSONArray.<init>(JSONArray.java:96)
at org.json.JSONArray.<init>(JSONArray.java:108)
Can anybody help me.
Thanks
Based on your comment to answer 1, you can do is
Your response
{"message":"No Results found!","status":"false"}
is not an array. It is an object. UseJSONObject
instead ofJSONArray
in your code.TIP: Arrays are wrapped in square brackets[ ] and objects are wrapped in curly braces{}.
I fix this issue by writing following code [ courtesy @Optional ]