Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

java - How to Get JSON Array Within JSON Object?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I guess this will help you.

JSONObject jsonObj = new JSONObject(jsonStr);

JSONArray ja_data = jsonObj.getJSONArray("data");
int length = jsonObj.length();
for(int i=0; i<length; i++) {
  JSONObject jsonObj = ja_data.getJSONObject(i);
  Toast.makeText(this, jsonObj.getString("Name"), Toast.LENGTH_LONG).show();

  // getting inner array Ingredients
  JSONArray ja = jsonObj.getJSONArray("Ingredients");
  int len = ja.length();

  ArrayList<String> Ingredients_names = new ArrayList<>();
  for(int j=0; j<len; j++) {
    JSONObject json = ja.getJSONObject(j);
    Ingredients_names.add(json.getString("name"));
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...