JSONArray cannot be converted to JSONObject error
Change
JSONObject data = jsonObj.getJSONObject("data");
to
JSONArray data = jsonObj.getJSONArray("data");
As value of data is JsonArray not JSONObject.
And to Get individual Ids and Field Names, you should loop through this JSONArray, as follows:
for(int i=0; i<data.length(); i++)
{
JSONObject obj=data.getJSONObject(i);
String id = obj.getString("Id");
String value = obj.getString("FieldName");
Log.d("Item name: ", value);
}
Type JSONArray cannot be converted to JSONObject
This is the solution that i found, thanks for all guys ♥ ..
private void jsonParse(){ String url="http://192.168.56.1:8095/rest/workouts/all"; JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET,url,null, new Response.Listener<JSONArray>() { @Override public void onResponse(JSONArray jsonArray) { try { for(int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonobject = jsonArray.getJSONObject(i);
String title = jsonobject.getString("title"); String goal = jsonobject.getString("goal");
txtv.append(title + ", " + goal +"\n\n"); } } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) {
} }); mQueue.add(jsonArrayRequest); }
items of type org.json.JSONArray cannot be converted to JSONObject
A JSONArray
is an array of JSONObject
s. Your items
json node is an array, not an object. Replace
JSONObject snippet = jsonObject.getJSONObject("items");
with
JSONArray snippet = jsonObject.getJSONArray("items");
Then from your snippet array you can get the item object you need
JSONObject item = snippet.getJSONObject(0);
And from there
String t = item.getString("rating");
Getting ''org.json.JSONArray cannot be converted to JSONObject''
The prolem here is that your website response body object is a JSONArray
[
{
"status": "Success",
"code": 1313,
"msg": "Request completed successfully"
}
]
So you get the exception because in the response handler you want a JSONObject
and you can't cast a JSONArray
to a JSONObject
.
What your server (website) have to return to you is a root JSONObject
and then in its node tree it could have JSONArray
, but the root must be a JSONObject
.
So fix your server side code so that it returns:
{
"status": "Success",
"code": 1313,
"msg": "Request completed successfully"
}
org.json.jsonarray cannot be converted to jsonobject error
Relevant code to change - in doInBackground()
of MainActivity.java
:
JSONObject eventDetails = parent.getJSONObject("event");
to:
JSONArray eventDetails = parent.getJSONArray("event");
Related Topics
Flutter: How to Fix "A Renderflex Overflowed by Pixels " Error
Call an Activity Method from a Broadcastreceiver Class
"Time Since/Ago" Library for Android/Java
How to Programmatically Include Layout in Android
Android Fragment Oncreateview VS. Onactivitycreated
Getting Error "Gradle Dsl Method Not Found: 'Compile()'" When Syncing Build.Gradle
Android - Choose File Button in Webview
Eclipse Can't Find Android.Support.V4.Widget.Swiperefreshlayout
Automatic Versioning of Android Build Using Git Describe with Gradle
How to Keep The Oauth Consumer Secret Safe, and How to React When It's Compromised