0

Hi All I am trying to parse a JSON String that contains a multidimensional array. I keep getting the following error in the logs -> W/System.err: org.json.JSONException: No value for nutrients and this error corresponds to the following line -> JSONArray jsonArray1 = jsonObject.getJSONArray("nutrients");

Can someone please help me with this issue. thanks in advance

The JSON String I am trying to parse ->

{
"results": [
    {
        "id": 654959,
        "title": "Pasta With Tuna",
        "image": "https://spoonacular.com/recipeImages/654959-312x231.jpg",
        "imageType": "jpg",
        "nutrition": {
            "nutrients": [
                {
                    "title": "Calories",
                    "amount": 420.823,
                    "unit": "kcal"
                },
                {
                    "title": "Protein",
                    "amount": 24.4751,
                    "unit": "g"
                },
                {
                    "title": "Fat",
                    "amount": 10.3277,
                    "unit": "g"
                },
                {
                    "title": "Carbohydrates",
                    "amount": 57.6915,
                    "unit": "g"
                }
            ]
        }
    }

My Java code to parse it ->

                            try {
                            JSONObject jsonObject = new JSONObject(response);
                            JSONArray jsonArray = jsonObject.getJSONArray("results");

                            for (int i = 0; i < jsonArray.length(); i++) {
                                int id = jsonArray.getJSONObject(i).getInt("id");
                                String title = jsonArray.getJSONObject(i).getString("title");
                                String image = jsonArray.getJSONObject(i).getString("image");
                                Log.i("a", "" + title);
                                JSONArray jsonArray1 = jsonObject.getJSONArray("nutrients");
                                for(int e = 0; e < jsonArray1.length(); e++) {
                                    JSONObject jsonObject1 = jsonArray1.getJSONObject(e);
                                    double calories = jsonObject1.getDouble("amount");
                                    Log.i("calories", "" + calories);
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
6
  • And why you don't access nutrition? I might help if you wouldn't ignore that. Commented Jan 11, 2021 at 16:25
  • is nutrients not the array ? I get this error when I try access nutrition W/System.err: org.json.JSONException: No value for nutrition Commented Jan 11, 2021 at 16:27
  • @MartinZeitler I think I understand you are saying to access nutrition then access nutrients from that? Commented Jan 11, 2021 at 16:29
  • @MartinZeitler thank you for your advice I took your guidance into consideration and fixing my code to this allowed me to get the nutrition object jsonArray.getJSONObject(i).getJSONObject("nutrition").getJSONArray("nutrients") Commented Jan 11, 2021 at 16:39
  • You first have to get the Object and then the Array it contains. Commented Jan 11, 2021 at 16:39

1 Answer 1

1

Your JSON is invalid.

Here is a valid one

{
"results": [
    {
        "id": 654959,
        "title": "Pasta With Tuna",
        "image": "https://spoonacular.com/recipeImages/654959-312x231.jpg",
        "imageType": "jpg",
        "nutrition": {
            "nutrients": [
                {
                    "title": "Calories",
                    "amount": 420.823,
                    "unit": "kcal"
                },
                {
                    "title": "Protein",
                    "amount": 24.4751,
                    "unit": "g"
                },
                {
                    "title": "Fat",
                    "amount": 10.3277,
                    "unit": "g"
                },
                {
                    "title": "Carbohydrates",
                    "amount": 57.6915,
                    "unit": "g"
                }
            ]
        }
    }
  ]
}

Then your Java code

try {
    JSONObject jsonObject = new JSONObject(response);
    JSONArray jsonArray = jsonObject.getJSONArray("results");
    JSONObject resultsObject=jsonArray.getJSONObject(0);

    int id = resultsObject.getInt("id");
    String title = resultsObject.getString("title");
    String image =resultsObject.getString("image");
    Log.i("a", "" + title);

    JSONObject nutrition = resultsObject.getJSONObject("nutrition");
    JSONArray nutrients = nutrition.getJSONArray("nutrients");

    for(int e = 0; e < nutrients.length(); e++) {
        JSONObject jsonObject1 = nutrients.getJSONObject(e);
        double calories = jsonObject1.getDouble("amount");
        Log.i("calories", "" + calories);
    }
 } catch (JSONException e) {
     e.printStackTrace();
 }

Hope Helpful!

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks so much for your help. After a few painful hours I was able to parse it and store the objects in the database :)
Accepted your answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.