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

Ich möchte den Nährstoff aus den JSON-Daten erhalten. Kann mir jemand dabei helfenJson Parsing von JSON-Array

Antwort

0

Siehe this Antwort für die Analyse von JSON in Java.

Nach der Visualisierung der JSON Sie hochgeladen here, habe ich den Code, der die Nährstoff-Zeichenfolge druckt implementiert. Sie können auch auf andere Parameter zugreifen.

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