Eugene H
Eugene H

Reputation: 3568

Getting all of the requested JSONObjects of a JSONArray. It is only displaying the last JSONObject

JSON is only displaying the last object '100 g' of "serving_description" in the JSON Formatted Data, See below. instead of all of the "serving_description" objects.

The Array is serving If you look at the JSON Formatted Data below you will see that there are multiple "serving_description" 's.

I am trying to get the "serving_description" of all of the the available options to display instead of the last object in which it is displaying. How do I display all of the "serving_descriptions"?

I believe the error lies in, but I can be wrong, that is why I am asking :

for (int n = 0; n < foodName.length(); n++) {
    JSONObject object = foodName.getJSONObject(n);
    String shit = object.getString("serving_description");
    Log.v("FATSEC", "" + shit);
    ret = shit + "";
}

Class, AsyncTask

        @Override
        public void onClick(View v) {
            new AsyncTask<String, String, String>() {

                @Override
                protected String doInBackground(String... arg0) {
                    search = (EditText) findViewById(R.id.editText1);

                    String SEARCH = search.getText().toString();
                    JSONObject food = getFood(SEARCH);
                    Log.v("FATSEC", "TEST");
                    String ret = "";

                    try {

                        JSONArray foodName = food.getJSONObject("food")
                                .getJSONObject("servings")
                                .getJSONArray("serving");

                        for (int n = 0; n < foodName.length(); n++) {
                            JSONObject object = foodName.getJSONObject(n);
                            String shit = object
                                    .getString("serving_description");
                            Log.v("FATSEC", "" + shit);
                            ret = shit + "";
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    return ret;
                }

                @Override
                protected void onPostExecute(String result) {
                    super.onPostExecute(result);
                    // ans.setText("# of Servings: " + result);
                    ans.setText("Servings: " + result);

                }

            }.execute();

        }
    });

Formatted JSON DATA

{  
   "servings":{  
      "serving":[  
         {  
            "vitamin_a":"0",
            "calcium":"2",
            "serving_description":"1 cup cooked",
            "vitamin_c":"0",
            "carbohydrate":"44.08",
            "metric_serving_unit":"g",
            "fat":"0.44",
            "sodium":"577",
            "polyunsaturated_fat":"0.119",
            "fiber":"0.6",
            "cholesterol":"0",
            "iron":"10",
            "serving_id":"16834",
            "protein":"4.20",
            "monounsaturated_fat":"0.138",
            "potassium":"55",
            "number_of_units":"1.000",
            "calories":"204",
            "measurement_description":"cup, cooked",
            "saturated_fat":"0.120",
            "metric_serving_amount":"158.000",
            "sugar":"0.08",
            "serving_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/rice-white-cooked-regular?portionid=16834&portionamount=1.000"
         },
         {  
            "vitamin_a":"0",
            "calcium":"6",
            "serving_description":"1 cup, dry, yields",
            "vitamin_c":"0",
            "carbohydrate":"159.03",
            "metric_serving_unit":"g",
            "fat":"1.60",
            "sodium":"2080",
            "polyunsaturated_fat":"0.429",
            "fiber":"2.3",
            "cholesterol":"0",
            "iron":"38",
            "serving_id":"15284",
            "protein":"15.16",
            "monounsaturated_fat":"0.497",
            "potassium":"200",
            "number_of_units":"1.000",
            "calories":"735",
            "measurement_description":"cup, dry, yields",
            "saturated_fat":"0.432",
            "metric_serving_amount":"570.000",
            "sugar":"0.29",
            "serving_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/rice-white-cooked-regular?portionid=15284&portionamount=1.000"
         },
         {  
            "vitamin_a":"0",
            "calcium":"1",
            "serving_description":"1 oz, dry, yields",
            "vitamin_c":"0",
            "carbohydrate":"24.27",
            "metric_serving_unit":"g",
            "fat":"0.24",
            "sodium":"318",
            "polyunsaturated_fat":"0.065",
            "fiber":"0.3",
            "cholesterol":"0",
            "iron":"6",
            "serving_id":"18252",
            "protein":"2.31",
            "monounsaturated_fat":"0.076",
            "potassium":"30",
            "number_of_units":"1.000",
            "calories":"112",
            "measurement_description":"oz, dry, yields",
            "saturated_fat":"0.066",
            "metric_serving_amount":"87.000",
            "sugar":"0.04",
            "serving_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/rice-white-cooked-regular?portionid=18252&portionamount=1.000"
         },
         {  
            "vitamin_a":"0",
            "calcium":"1",
            "serving_description":"1 serving (105 g)",
            "vitamin_c":"0",
            "carbohydrate":"29.30",
            "metric_serving_unit":"g",
            "fat":"0.29",
            "sodium":"383",
            "polyunsaturated_fat":"0.079",
            "fiber":"0.4",
            "cholesterol":"0",
            "iron":"7",
            "serving_id":"17592",
            "protein":"2.79",
            "monounsaturated_fat":"0.092",
            "potassium":"37",
            "number_of_units":"1.000",
            "calories":"135",
            "measurement_description":"serving (105g)",
            "saturated_fat":"0.080",
            "metric_serving_amount":"105.000",
            "sugar":"0.05",
            "serving_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/rice-white-cooked-regular?portionid=17592&portionamount=1.000"
         },
         {  
            "vitamin_a":"0",
            "calcium":"1",
            "serving_description":"100 g",
            "vitamin_c":"0",
            "carbohydrate":"27.90",
            "metric_serving_unit":"g",
            "fat":"0.28",
            "sodium":"365",
            "polyunsaturated_fat":"0.075",
            "fiber":"0.4",
            "cholesterol":"0",
            "iron":"7",
            "serving_id":"53181",
            "protein":"2.66",
            "monounsaturated_fat":"0.087",
            "potassium":"35",
            "number_of_units":"100.000",
            "calories":"129",
            "measurement_description":"g",
            "saturated_fat":"0.076",
            "metric_serving_amount":"100.000",
            "sugar":"0.05",
            "serving_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/rice-white-cooked-regular?portionid=53181&portionamount=100.000"
         }
      ]
   },
   "food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/rice-white-cooked-regular",
   "food_type":"Generic",
   "food_name":"White Rice",
   "food_id":"4501"
}

Upvotes: 0

Views: 62

Answers (2)

Matthew Pape
Matthew Pape

Reputation: 1691

Each iteration of the loop overrides the value of ret so that you are never able to accumulate them. For this reason, when you return it has the last value you stored there and none of the previous values that were wiped out. Consider using a StringBuilder.

StringBuilder builder = new StringBuilder();

...

for (int n = 0; n < foodName.length(); n++) {
    JSONObject object = foodName.getJSONObject(n);
    String shit = object.getString("serving_description");
    Log.v("FATSEC", "" + shit);
    builder.append(shit).append("\n");
}

...

return builder.toString();

Upvotes: 1

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132982

Use + to append all serving_description value in ret as:

   ret += shit + "\n\n";

Upvotes: 2

Related Questions