This is my json
{ "data": [ { "id": 1, "Name": "Choc Cake", "Image": "1.jpg", "Category": "Meal", "Method": "", "Ingredients": [ { "name": "1 Cup Ice" }, { "name": "1 Bag Beans" } ] }, { "id": 2, "Name": "Ice Cake", "Image": "dfdsfdsfsdfdfdsf.jpg", "Category": "Meal", "Method": "", "Ingredients": [ { "name": "1 Cup Ice" } ] } ] }
And this is how I get the identifier, the name of this part works great in getting the first part of JSON
//getting whole json string JSONObject jsonObj = new JSONObject(jsonStr); //extracting data array from json string JSONArray ja_data = jsonObj.getJSONArray("data"); int length = jsonObj .length(); //loop to get all json objects from data json array for(int i=0; i<length; i++) { JSONObject jObj = ja_data.getJSONObject(i); Toast.makeText(this, jObj.getString("Name").toString(), Toast.LENGTH_LONG).show(); // getting inner array Ingredients JSONArray ja = jObj.getJSONArray("Ingredients"); int len = ja.length(); }
Now I'm trying to get the ingredients not sure how this is what I tried
//getting whole json string JSONObject jsonObj = new JSONObject(jsonStr); //extracting data array from json string JSONArray ja_data = jsonObj.getJSONArray("data"); int length = jsonObj .length(); //loop to get all json objects from data json array for(int i=0; i<length; i++) { JSONObject jObj = ja_data.getJSONObject(i); Toast.makeText(this, jObj.getString("Name").toString(), Toast.LENGTH_LONG).show(); // getting inner array Ingredients JSONArray ja = jObj.getJSONArray("Ingredients"); int len = ja.length(); // getting json objects from Ingredients json array for(int j=0; j<len; j++) { JSONObject json = ja.getJSONObject(j); Toast.makeText(this, json.getString("name").toString(), Toast.LENGTH_LONG).show(); } }
But it doesn’t work, how can I get it?
source share