简体   繁体   中英

Issue with a for loop over JSON

I seem to be having an issue looping through a JSONObject

The following loop is within a try catch. I am trying to loop through returned JSON, but it skips

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();
}

Response 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",

Code within the onPostExecute. even with the changes still ignores the inners and moves straight to the catch.

@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));
}

You need to access the recipe array first

Try this

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM