【问题标题】:Issue with a for loop over JSONJSON 上的 for 循环问题
【发布时间】:2017-12-21 00:45:28
【问题描述】:

我似乎在通过 JSONObject 循环时遇到问题

以下循环在 try catch 中。我正在尝试遍历返回的 JSON,但它会跳过

try {
    JSONObject jsonObject = new JSONObject(result);
    // Extract data from json and store into ArrayList as class objects
    for(int i = 0; i < jsonObject.length(); i++){
        JSONObject jD = jsonObject.getJSONObject(i).;
        DataRecipes recipeData = new DataRecipes();
        recipeData.image_url = jD.getString("image_url");
        recipeData.title = jD.getString("title");
        recipeData.publisher = jD.getString("publisher");
        data.add(recipeData);
    }
} catch (JSONException e) {
    Toast.makeText(Main_Food.this, e.toString(), Toast.LENGTH_LONG).show();
}

响应 JSON

{"count": 30, "recipes": [{"publisher": "Closet Cooking", "f2f_url": "http://food2fork.com/view/35382", "title": "Jalapeno Popper Grilled Cheese Sandwich", "source_url": "http://www.closetcooking.com/2011/04/jalapeno-popper-grilled-cheese-sandwich.html", "recipe_id": "35382", "image_url": "http://static.food2fork.com/Jalapeno2BPopper2BGrilled2BCheese2BSandwich2B12B500fd186186.jpg", "social_rank": 100.0, "publisher_url": "http://closetcooking.com"}, {"publisher": "The Pioneer Woman",

onPostExecute 中的代码。 即使进行了更改,仍然会忽略内部并直接进行捕捉。

@Override
protected void onPostExecute(String result) {
    List<DataRecipes> data = new ArrayList<>();

    try {
        JSONObject jsonObject = new JSONObject(result);
        JSONArray jsonArrayRecipe = jsonObject.getJSONArray("recipes");

        for(int i = 0; i < jsonArrayRecipe.length(); i++){

            JSONObject jD = jsonArrayRecipe.getJSONObject(i);
            DataRecipes recipeData = new DataRecipes();

            recipeData.image_url = jD.getString("image_url");
            recipeData.title = jD.getString("title");
            recipeData.publisher = jD.getString("publisher");

            data.add(recipeData);
        }
    } catch (JSONException e) {
        Toast.makeText(Main_Food.this, e.toString(), Toast.LENGTH_LONG).show();
    }
    // Setup and Handover data to recyclerview
    mRVRecipePrice = findViewById(R.id.jsonText);
    mAdapter = new AdapterRecipe(Main_Food.this, data);
    mRVRecipePrice.setAdapter(mAdapter);
    mRVRecipePrice.setLayoutManager(new LinearLayoutManager(Main_Food.this));
}

【问题讨论】:

  • 请在您的问题中添加您的 json 响应。
  • 对不起正确的代码如下
  • 请检查我的回答。
  • 请不要为您的异常干杯。将它们记录到 logcat,然后将错误复制到问题中
  • 你好Guruji,谢谢你的回复,恐怕还是没有进入try catch。

标签: java android json for-loop


【解决方案1】:

你需要先访问配方数组

试试这个

try {
      JSONObject jsonObject = new JSONObject(result);
      JSONArray jsonArrayrecipe = jsonObject .getJSONArray("recipes");

      for(int i = 0; i < jsonArrayrecipe .length(); i++){
         JSONObject jD = jsonArrayrecipe .getJSONObject(i);
         DataRecipes recipeData = new DataRecipes();
         recipeData.image_url = jD.getString("image_url");
         recipeData.title = jD.getString("title");
         recipeData.publisher = jD.getString("publisher");
         data.add(recipeData);
     }
 } catch (JSONException e) {
      Toast.makeText(Main_Food.this, e.toString(), Toast.LENGTH_LONG).show();
}

【讨论】:

  • @mrtmerlin 请将此代码设置为您的 AsynTask doInBackground() 方法。
猜你喜欢
  • 1970-01-01
  • 2019-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-05
  • 1970-01-01
  • 2013-07-14
  • 2021-05-10
相关资源
最近更新 更多