Skip to content Skip to sidebar Skip to footer

Android Java Type Org.json.jsonobject Cannot Be Converted To Jsonarray

I am trying to connect to an Api link, read and construct all values in a object list. I call the AsyncTask from my activity. I checked the list values and they are all correct. wa

Solution 1:

Change JSONArray list = new JSONArray(Jo_result.getString("result"));

To JSONObject list = new JSONObject(Jo_result.getString("result"));

Your string is contained between {} which makes it an object.

Keep in mind this

{} = Json Object

[] = Json Array

UPDATE

When you do this

JSONObject resultsObject = list.getJSONObject(i);

it's expecting another object within the main object, for example :

{  // <-- main object

   i : { <-- object failing
     // ...
   }

}

That's what is the code expecting, where i is from the loop, but that doesn't exist and besides that, the key must be a string, for example

{  // <-- main object

   string : {
     // ...
   }

}

So, what I recommend you : remove the for loop since you don't need it anymore and call your variables within the json like this list.getString("data_id") since it's the list which contains your json object.

Hope it helps.

Solution 2:

Did you try Retrofit ? it would be so much easier for you case. It handle Get/Post request & deserialize JSON into Java Objects with GSON

square.github.io/retrofit/

Post a Comment for "Android Java Type Org.json.jsonobject Cannot Be Converted To Jsonarray"