2016-04-04 24 views
-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 
      } 
     ] 
     } 
    ] 
    } 
} 

Besin değerini json verilerinden almak istiyorum. Herhangi biri bana buJson dizisinin ayrıştırma Json dizisi

cevap

0

ile yardımcı olabilir. Java ayrıştırma json için this yanıtına bakın.

here yüklediğiniz json'u görüntüledikten sonra, besin dizesini yazdıran kodu uyguladı. Benzer diğer parametrelere de erişebilirsiniz.

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