-4

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"



             }

             ]
             }
halfer
  • 19,824
  • 17
  • 99
  • 186
Darshi
  • 3
  • 1

3 Answers3

0

Use this:

Log.d("Data", obj.toString());
Chandra Kumar
  • 4,127
  • 1
  • 17
  • 25
  • 1
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Patrick Hund Aug 19 '17 at 13:54
0

you need to get it in an String type variable let say responseString than follow the code

    JSONObject  jsonRootObject = new JSONObject(responseString);
    JSONArray jsonArray = jsonRootObject.optJSONArray("response");
    String result = "", cat_id = "", cat_name = "", cat_status = ""; 
    for(int i=0; i < jsonArray.length(); i++){  

                            JSONObject jsonObject = jsonArray.getJSONObject(i);  
                            cat_id = jsonObject.optString("cat_id").toString();
                            cat_name = jsonObject.optString("cat_name").toString();
                            cat_status = jsonObject.optString("cat_status").toString();

    result += cat_id+"\n"+cat_name+"\n"+cat_status+"\n";                           
    }
textView_to_display_data_on_screen.setText(result);

it will print all your data in to textView (i.e here textView_to_display_data_on_screen).

Arpit Prajapati
  • 367
  • 2
  • 16
0

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

}
AskNilesh
  • 67,701
  • 16
  • 123
  • 163