2017-10-27 2 views
-2

Ich brauche den entsprechenden "Wert" basierend auf der "attr_id" aus "full_nutrients" -Array in der JSON-Antwort unten mit Java, wie kann das erreicht werden? Zum Beispiel möchte ich den Wert erhalten, wenn "attr_id" == 205.Wert von JSON Response mit verschachtelten Namen Wertepaare basierend auf einem Namen Wert in Java

JSON Antwort:

"foods": [ 
    { 
     "food_name": "chicken noodle soup", 
     "brand_name": null, 
     "serving_qty": 1, 
     "serving_unit": "cup", 
     "serving_weight_grams": 248, 
     "nf_calories": 62, 
     "nf_total_fat": 2.36, 
     "nf_saturated_fat": 0.65, 
     "nf_cholesterol": 12.4, 
     "nf_sodium": 865.52, 
     "nf_total_carbohydrate": 7.32, 
     "nf_dietary_fiber": 0.5, 
     "nf_sugars": 0.67, 
     "nf_protein": 3.15, 
     "nf_potassium": 54.56, 
     "nf_p": 42.16, 
     "full_nutrients": [ 
      { 
       "attr_id": 203, 
       "value": 3.1496 
      }, 
      { 
       "attr_id": 204, 
       "value": 2.356 
      }, 
      { 
       "attr_id": 205, 
       "value": 7.316 
      }, 
      { 
       "attr_id": 207, 
       "value": 2.5048 
      }], 
     } 
+0

Bitte gehen Sie durch https://www.tutorialspoint.com/android/android_json_parser.htm –

+0

ich einzelne Werte bekommen, Das Problem, das ich habe, ist der Name Wert Paare, wenn attr_id == 205, möchte ich 7.316 zurückgeben. Das Tutorial bietet keine solchen Beispiele, vielleicht ist das nicht möglich und ich muss alles in einem Array speichern und tun, wenn die Anweisungen jedes einzelne Paar durchlaufen, was ineffizient erscheint. – John

Antwort

3

Ihr json ungültig ist.

Sie können dies ändern.

{"foods": [ 

{ 
    "food_name": "chicken noodle soup", 
    "brand_name": null, 
    "serving_qty": 1, 
    "serving_unit": "cup", 
    "serving_weight_grams": 248, 
    "nf_calories": 62, 
    "nf_total_fat": 2.36, 
    "nf_saturated_fat": 0.65, 
    "nf_cholesterol": 12.4, 
    "nf_sodium": 865.52, 
    "nf_total_carbohydrate": 7.32, 
    "nf_dietary_fiber": 0.5, 
    "nf_sugars": 0.67, 
    "nf_protein": 3.15, 
    "nf_potassium": 54.56, 
    "nf_p": 42.16, 
    "full_nutrients": [ 
     { 
      "attr_id": 203, 
      "value": 3.1496 
     }, 
     { 
      "attr_id": 204, 
      "value": 2.356 
     }, 
     { 
      "attr_id": 205, 
      "value": 7.316 
     }, 
     { 
      "attr_id": 207, 
      "value": 2.5048 
     }], 
    } 
} 

Versuchen Sie, diese

try { 
     // if your response is { },you can use JSONObject 
     JSONObject jsonObject = new JSONObject(response); 
     // then find the foods tag in your json data 
     JSONArray foods = jsonObject.getJSONArray("foods"); 
     // loop for the JSONArray 
     for (int i = 0; i < foods.length(); i++) { 
      // getJSONObject from the index 
      JSONObject jsonObject1 = foods.getJSONObject(i); 
      // then get full_nutrients tag 
      JSONArray full_nutrients = jsonObject1.getJSONArray("full_nutrients"); 
      // loop for the JSONArray 
      for (int j = 0; j < full_nutrients.length(); j++) { 
       // getJSONObject from the index again 
       JSONObject jsonObject2 = full_nutrients.getJSONObject(i); 
       // get attr_id 
       String attr_id = jsonObject2.getString("attr_id"); 
       // get value 
       String value = jsonObject2.getString("value"); 
      } 
     } 
} catch (JSONException e) { 
     e.printStackTrace(); 
} 
+0

Dies wird nur den Wert von attr_id erhalten, ich muss den entsprechenden Wert erhalten. Also, wenn attr_id == 205 Ich möchte 7.316 bekommen. – John

+0

Sie können es überprüfen. – KeLiuyue

2

Try this:

function Double GetValue(String json_object, int attr_id) { 

    Double tResult = 0; 
    JSONObject reader = new JSONObject(json_object); 
    // Getting JSON Array node 
    JSONArray full_nutrients = reader.getJSONArray("full_nutrients"); 

    // looping through All full_nutrients 
    for (int i = 0; i < full_nutrients.length(); i++) { 
     JSONObject c = full_nutrients.getJSONObject(i); 
     if (c.getInt("attr_id") == attr_id) 
      tResult = c.getDouble("value"); 
    } 
    return tResult; 
} 
+0

Danke, das sieht so aus, als würde es funktionieren, habe auf eine Lösung gehofft, die kein Schleifen durch das gesamte Array erfordert, um einen einzelnen Wert zu erhalten. Ich weiß nicht, warum der Dienst so formatiert JSON zurückgibt. – John

Verwandte Themen