Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
175 views
in Technique[技术] by (71.8m points)

java - Parsing multidimensional JSON array ERROR

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...