-2

I need to get the corresponding "value" based on the "attr_id" from "full_nutrients" array in the JSON response below using Java, how can this be accomplished? For example I want to get the value when "attr_id" == 205.

JSON Response:

"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 }], } 
2
  • Please go through tutorialspoint.com/android/android_json_parser.htm Commented Oct 27, 2017 at 16:36
  • I can get single values, the issue I'm having is the name value pairs, if attr_id == 205, I want to return 7.316. The tutorial provides no such examples, maybe this isn't possible and I have to store everything in an array and do if statements going through every single pair, that seems inefficient. Commented Oct 27, 2017 at 16:52

2 Answers 2

3

Your json is invalid .

You can change to this .

{"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 }], } } 

Try this

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(); } 
Sign up to request clarification or add additional context in comments.

2 Comments

This will only get the value of attr_id, I need to get the corresponding value. So if attr_id == 205 I want to get 7.316.
You can check it .
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; } 

1 Comment

Thanks, this looks like it will work, was hoping for a solution that doesn't require looping through the entire array to get a single value. I don't know why the service returns JSON formatted like that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.