2

I have problems accessing data in a JSON string. What am I doing wrong?

Working:

JSONObject obj = new JSONObject("JSON-STRING");
JSONArray arr = obj.getJSONArray("weather");
System.out.println(arr.getJSONObject(0).get("description"); >> clear sky

Not working:

JSONObject obj = new JSONObject("JSON-STRING");
JSONArray arr = obj.getJSONArray("main");
System.out.println(arr.getJSONObject(0).get("temp"); >> 285.15

Exception:

org.json.JSONException: JSONObject["main"] is not a JSONArray.
    at org.json.JSONObject.getJSONArray(JSONObject.java:622)
    at main.SmartHomeBot.onUpdateReceived(SmartHomeBot.java:47)
    at org.telegram.telegrambots.updatesreceivers.DefaultBotSession$HandlerThread.run(DefaultBotSession.java:274)

The JSON-String:

{
    "coord": {
        "lon": 6.55,
        "lat": 51.27
    },
    "weather": [{
            "id": 800,
            "main": "Clear",
            "description": "clear sky",
            "icon": "01d"
        }
    ],
    "base": "stations",
    "main": {
        "temp": 285.15,
        "pressure": 1034,
        "humidity": 30,
        "temp_min": 285.15,
        "temp_max": 285.15
    },
    "visibility": 10000,
    "wind": {
        "speed": 2.6
    },
    "clouds": {
        "all": 0
    },
    "dt": 1492705200,
    "sys": {
        "type": 1,
        "id": 4909,
        "message": 0.2825,
        "country": "DE",
        "sunrise": 1492662386,
        "sunset": 1492713582
    },
    "id": 2808559,
    "name": "Willich",
    "cod": 200
}
3
  • 3
    {..} represents object, [..] represents array. As you see "main": {...} holds an object not an array, which is why getJSONArray("main") is causing problems. Commented Apr 20, 2017 at 17:58
  • Voting to close as typo. Nothing more to explain here. Commented Apr 20, 2017 at 18:02
  • JSONObject obj = new JSONObject(JSON-STRING); JSONObject arr = obj.getJSONObject("main"); System.out.println(arr.get("temp")); fixed it for me. Thanks! Commented Apr 20, 2017 at 18:04

3 Answers 3

1

You get the error because weather is the array of the multiple weather and main is the single object.

Difference between both is shown below:

 "weather": [{
        "id": 800,
        "main": "Clear",
        "description": "clear sky",
        "icon": "01d"
    }
],

and

"main": {
    "temp": 285.15,
    "pressure": 1034,
    "humidity": 30,
    "temp_min": 285.15,
    "temp_max": 285.15
},

So in the JSON "weather" : [{....}, {....}, {....}] [] show that weather is the array.

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

Comments

0

In your parent json value for key "weather" represents a JSONArray but value for key "main" represents a JSONObject not JSONArray.

To fetch the data from JsonObject you should do like below

JSONObject mainObj = obj.getJSONObject("main");
System.out.println(mainObj.get("temp"));

Comments

0
String jsonobj = "{\n    \"coord\": {\n        \"lon\": 6.55,\n        \"lat\": 51.27\n    },\n    \"weather\": [{\n            \"id\": 800,\n            \"main\": \"Clear\",\n            \"description\": \"clear sky\",\n            \"icon\": \"01d\"\n        }\n    ],\n    \"base\": \"stations\",\n    \"main\": {\n        \"temp\": 285.15,\n        \"pressure\": 1034,\n        \"humidity\": 30,\n        \"temp_min\": 285.15,\n        \"temp_max\": 285.15\n    },\n    \"visibility\": 10000,\n    \"wind\": {\n        \"speed\": 2.6\n    },\n    \"clouds\": {\n        \"all\": 0\n    },\n    \"dt\": 1492705200,\n    \"sys\": {\n        \"type\": 1,\n        \"id\": 4909,\n        \"message\": 0.2825,\n        \"country\": \"DE\",\n        \"sunrise\": 1492662386,\n        \"sunset\": 1492713582\n    },\n    \"id\": 2808559,\n    \"name\": \"Willich\",\n    \"cod\": 200\n}";
JSONObject obj = new JSONObject(jsonobj);
JSONObject jsonObject = obj.getJSONObject("main");
System.out.println(jsonObject.get("temp"));

1 Comment

A line that is nearly 900 characters long. Isn't there a better way?

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.