web-dev-qa-db-ja.com

JSONオブジェクト内でJSON配列を取得する方法は?

これは私の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"
                }
            ]
        }
    ]
}

そして、これは私がIDを取得する方法です、この部分は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();


} 

今、私はこれをどのようにしようとしているかわからない成分を取得しようとしています

//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();
    }
} 

しかし、どのように入手できますか?

11
Tash Nar

これが役に立つと思います。

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").toString(), 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").toString());
  }
}
9
user5345196
JSONObject jsonObject =new JSONObject(jsonStr);
JSONArray jsonArray = jsonObject.getJSONArray("data");
for(int i=0;i<jsonArray.length;i++){
    JSONObject json = jsonArray.getJSONObject(i);
    String id = json.getString("id");
    String name=json.getString("name");
    JSONArray ingArray = json.getJSONArray("Ingredients") // here you are going   to get ingredients
    for(int j=0;j<ingArray.length;j++){
        JSONObject ingredObject= ingArray.getJSONObject(j);
        String ingName = ingredObject.getString("name");//so you are going to get   ingredient name
        Log.e("name",ingName); // you will get 
   }
}
7
Shadow

解決済み、文字列の配列リストを使用して成分から名前を取得します。以下のコードを使用:

JSONObject jsonObj = new JSONObject(jsonStr);

//extracting data array from json string
JSONArray ja_data = jsonObj.getJSONArray("data");
int length = ja_data.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();
    ArrayList<String> Ingredients_names = new ArrayList<>();
    for(int j=0; j<len; j++){
        JSONObject json = ja.getJSONObject(j);
        Ingredients_names.add(json.getString("name").toString());
    }
}
5
Ronak Joshi

int length = jsonObj.length();int length = ja_data.length();でなければなりません

1
JSONObject jsonObj = new JSONObject(jsonString);

JSONArray jArray = jsonObj.getJSONArray("data");
int length = jArray.length();

   for(int i=0; i<length; i++)
      {
        JSONObject jObj = jArray.getJSONObject(i);
        String id = jObj.optString("id");
        String name=jObj.optString("name");

        JSONArray ingredientArray = jObj.getJSONArray("Ingredients");
        int size = ingredientArray.length();
        ArrayList<String> Ingredients = new ArrayList<>();

            for(int j=0; j<size; j++)
               {
                    JSONObject json = ja.getJSONObject(j);
                    Ingredients.add(json.optString("name"));
                }


       }
1