-2
{
  "report": {
    "sr": "28",
    "groups": "All groups",
    "subset": "All foods",
    "end": 1,
    "start": 0,
    "total": 1,
    "foods": [
      {
        "ndbno": "01009",
        "name": "Cheese, cheddar",
        "weight": 132.0,
        "measure": "1.0 cup, diced",
        "nutrients": [
          {
            "nutrient_id": "203",
            "nutrient": "Protein",
            "unit": "g",
            "value": "30.19",
            "gm": 22.87
          },
          {
            "nutrient_id": "205",
            "nutrient": "Carbohydrate, by difference",
            "unit": "g",
            "value": "4.08",
            "gm": 3.09
          },
          {
            "nutrient_id": "301",
            "nutrient": "Calcium, Ca",
            "unit": "mg",
            "value": "937",
            "gm": 710.0
          },
          {
            "nutrient_id": "208",
            "nutrient": "Energy",
            "unit": "kcal",
            "value": "533",
            "gm": 404.0
          },
          {
            "nutrient_id": "303",
            "nutrient": "Iron, Fe",
            "unit": "mg",
            "value": "0.18",
            "gm": 0.14
          },
          {
            "nutrient_id": "291",
            "nutrient": "Fiber, total dietary",
            "unit": "g",
            "value": "0.0",
            "gm": 0.0
          }
        ]
      }
    ]
  }
}

I want to get the nutrient from the json data. Can any one help me with this

RaminS
  • 2,208
  • 4
  • 22
  • 30

1 Answers1

0

Refer to this answer for parsing json in java.

After visualizing the json you uploaded here, I have implemented the code that prints the nutrient string. You can similarly access other parameters.

String json = "{ \"report\": { \"sr\": \"28\", \"groups\": \"All groups\", \"subset\": \"All foods\", \"end\": 1, \"start\": 0, \"total\": 1, \"foods\": [ { \"ndbno\": \"01009\", \"name\": \"Cheese, cheddar\", \"weight\": 132.0, \"measure\": \"1.0 cup, diced\", \"nutrients\": [ { \"nutrient_id\": \"203\", \"nutrient\": \"Protein\", \"unit\": \"g\", \"value\": \"30.19\", \"gm\": 22.87 }, { \"nutrient_id\": \"205\", \"nutrient\": \"Carbohydrate, by difference\", \"unit\": \"g\", \"value\": \"4.08\", \"gm\": 3.09 }, { \"nutrient_id\": \"301\", \"nutrient\": \"Calcium, Ca\", \"unit\": \"mg\", \"value\": \"937\", \"gm\": 710.0 }, { \"nutrient_id\": \"208\", \"nutrient\": \"Energy\", \"unit\": \"kcal\", \"value\": \"533\", \"gm\": 404.0 }, { \"nutrient_id\": \"303\", \"nutrient\": \"Iron, Fe\", \"unit\": \"mg\", \"value\": \"0.18\", \"gm\": 0.14 }, { \"nutrient_id\": \"291\", \"nutrient\": \"Fiber, total dietary\", \"unit\": \"g\", \"value\": \"0.0\", \"gm\": 0.0 } ] } ] } }";
JSONObject obj = new JSONObject(json);
JSONArray foods = obj.getJSONObject("report").getJSONArray("foods");
JSONObject food = foods.getJSONObject(0);
JSONArray nutrients = food.getJSONArray("nutrients");

for (int i = 0; i < nutrients.length(); i++) {
    String post_id = nutrients.getJSONObject(i).getString("nutrient");
    System.out.println(post_id);
}
Community
  • 1
  • 1