Darshi Darshi - 3 months ago 15
JSON Question

How can I print this type of JSON in Android?

How can I print this type of JSON object in Android? It include jsonArray inside jsonObject.

This is my PHP file output:

{
"response": [
{
"cat_id": "1",
"cat_name": "abc",
"cat_status": "1"
},
{
"cat_id": "2",
"cat_name": "abc",
"cat_status": "1"
},
{
"cat_id": "3",
"cat_name": "abc",
"cat_status": "1"



}

]
}

Answer Source

try this

fisrt get your JSONArray like below

JSONObject json = new JSONObject(JsonResponseString);// get JSONObject from respones 
JSONArray jArray = json.getJSONArray("response");   // get JSONArray from json object

now get your data from jsonArray like below code

for(int i=0; i<jArray.length(); i++){

  JSONObject json_data = jArray.getJSONObject(i);           // get your json object from array

  Log.d("cat_id", json_data.getString("cat_id"));          // get your cat_id

  Log.d("cat_name", json_data.getString("cat_name"));     // get your cat_name

  Log.d("cat_status", json_data.getString("cat_status"));// get your cat_status

}