1

I'm trying to parse this JSON,

{
  "listname": "red",
  "lists": [
    {
      "id": "01",
      "name": "paw",
      "list": [
        {
          "id": "A",
          "name": "pawa",
          "bar": "foo"
        },
        {
          "id": "B",
          "name": "pawb",
          "bar": "foo"
        }
      ]
    },
    {
      "id": "02",
      "name": "pew",
      "list": [
        {
          "id": "A",
          "name": "pewa",
          "bar": "foo"
        },
        {
          "id": "B",
          "name": "pewb",
          "bar": "foo"
        }
      ]
    },
    {
      "id": "03",
      "name": "piw",
      "list": [
        {
          "id": "A",
          "name": "piwa",
          "bar": "foo"
        },
        {
          "id": "B",
          "name": "piwb",
          "bar": "foo"
        }
      ]
    }
  ]
}

I put it on Asset Folder and I read it and it converts it to me to String since here all good, the problem is when I'm trying to get the name from each item of lists and trying to get all the names from the list I've tried it doing this :

JSONObject obj = new JSONObject(str);
JSONArray jsonMainArr = obj.getJSONArray("lists"); //first get the lists
for (int i = 0; i < jsonMainArr.length(); i++) { 
   JSONObject childJSONObject = jsonMainArr.getJSONObject(i);
   String name = childJSONObject.getString("name");
   Toast.makeText(context, name, Toast.LENGTH_SHORT).show();
}

JSONObject obj = new JSONObject(str);
JSONArray jsonMainArr = obj.getJSONArray("list"); //get the list
for (int i = 0; i < jsonMainArr.length(); i++) { 
   JSONObject childJSONObject = jsonMainArr.getJSONObject(i);
   String name = childJSONObject.getString("name");
   Toast.makeText(context, name, Toast.LENGTH_SHORT).show();
}

But it doesn't show anything... what I'm missing?

EDIT

This is how I read the JSON

 public static String loadJSONFromAsset(Context ctx, String str) {
    String json = null;
    try {

        InputStream is = ctx.getAssets().open(str);

        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        json = new String(buffer, "UTF-8");

    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return json;

}
10
  • I tried this in plain Java & it worked as expected. Check if there was something wrong with the context. Commented Jan 18, 2016 at 17:33
  • just debug it ...and check whether that array json has elements or not Commented Jan 18, 2016 at 17:36
  • Hello @MohammedAoufZOUAG :), I did it with Log and doesn't work either Commented Jan 18, 2016 at 17:38
  • @Skizo make sure that the execution flow reaches this code block & double check if str is actually populated with data. As I said, your code's logic is correct (I used sysouts instead of toasts & it worked), the problem is something else :) Commented Jan 18, 2016 at 17:41
  • @MohammedAoufZOUAG str is populated because I create a Toast and it shows all the JSON, so.... Commented Jan 18, 2016 at 17:42

1 Answer 1

2

Since your "list" is nested in the childJSONObject (from "lists") , nest your for loops to retrieve the this set of values (the JSONobject)

JSONObject obj = new JSONObject(str);
//first get the top-level "lists"
JSONArray jsonMainArr = obj.getJSONArray("lists");
for (int i = 0; i < jsonMainArr.length(); i++) { 
   JSONObject childJSONObject = jsonMainArr.getJSONObject(i);
   String name = childJSONObject.getString("name");
   Toast.makeText(context, name, Toast.LENGTH_SHORT).show();
   //get the inner "list" from childJSONObject
   JSONArray childJSONArray = childJSONObject.getJSONArray("list"); 
   for (int j = 0; j < childJSONArray.length(); j++) {
      JSONObject childJSONObjectB = childJSONArray.getJSONObject(j);
      String name = childJSONObjectB.getString("name");
      Toast.makeText(context, name, Toast.LENGTH_SHORT).show();
   }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your answer, but saddly it doesn't work, it still without showing nothing on my Toast...
Says org.json.JSONException: No value for lists
Dont know, Try logging the str from the first line. Its possible that it is empty?
Nope I show a Toast that shows the full JSON, well I'm gonna debug it again
can you explain me the reason why if I put other name on getJSONArray("list"); it doesn't work? seems like it only reads the first array and the rest does not...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.