Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Get JSON Array Within JSON Object?

Tags:

People also ask

What is JSON object and JSON array in Java?

JSONObject and JSONArray are the two common classes usually available in most of the JSON processing libraries. A JSONObject stores unordered key-value pairs, much like a Java Map implementation. A JSONArray, on the other hand, is an ordered sequence of values much like a List or a Vector in Java.

How do I make a JSON object with multiple arrays?

The JSON data is an object (basically an associative array). Indexed arrays use square brackets, [0,1,2] , while associative arrays use curly braces, {x:1,y:2,z:3} . Any of the data within the outermost object can be either type of array, but the outermost object itself has to use curly braces.


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 am getting the id and name, you can see this part works fine in returning JSON values that are not array values.

//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"), Toast.LENGTH_LONG).show();      // getting inner array Ingredients     JSONArray ja = jObj.getJSONArray("Ingredients");     int len = ja.length();   }  

Now i'm trying to get the values from the ingredients array and I am not sure how to get them? This is what I have been trying thus far.

//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"), 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"), Toast.LENGTH_LONG).show();     } }  

But doing it this way is not working, how can I get it to work?