2014-09-04 143 views
0

JSON仅在JSON格式数据中显示“serving_description”的最后一个对象'100g',请参见下文。而不是所有的“serving_description”对象。获取JSONArray的所有请求的JSONObjects。它仅显示最后一个JSONObject

的数组服务 如果你看一下JSON格式的数据下面你会看到,有多个“serving_description”的。

我想获得所有可用选项的“serving_description”来显示,而不是它显示的最后一个对象。我如何显示所有的“serving_descriptions”?

我认为错误在于,但我可能是错的,这就是为什么我问:

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 + ""; 
} 

类,的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(); 

     } 
    }); 

格式化JSON数据

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

回答

2

使用+追加在ret所有serving_description值:

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

工作。谢谢。 – 2014-09-04 18:05:41

+0

我如何将每个“serving_description”分隔成自己的字符串? – 2014-09-04 18:20:49

+0

@EugeneH:使用像ArrayList,HashMap等任何集合来分别存储每个'serving_description' – 2014-09-04 18:23:27

1

循环的每次迭代将覆盖ret值,让你永远无法积累他们。出于这个原因,当你返回时,它有你存储在那里的最后一个值,以及之前没有被清除的值。考虑使用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(); 
+0

这也适用。我有你的答案。 – 2014-09-04 18:04:21

+1

很高兴您找到了可行的解决方案。感谢您的投票。 – 2014-09-04 18:25:38

相关问题