org.json.JSONException: JSONObject["status"] is not a JSONObject

10,994

Solution 1

According to the getJSONObject() Javadoc, this method will throw an exception if the returned object isn't a true JSON object, which it isn't because "status" is a string. As such, try using data.getString("status").

Solution 2

The status field in the JSON document you have posted is not an object. In JSON, objects are enclosed in with {} brackets. The result node however, is a nested object which holds the status key/value pair. Try the following:

JSONObject data = new JSONObject(json.toString());
  if(data.getJSONObject("result").get("status").toString() != "ok" ) {
    return null;
  }
Share:
10,994
Z.Richard
Author by

Z.Richard

Updated on July 13, 2022

Comments

  • Z.Richard
    Z.Richard almost 2 years

    I am now currently using a weather API from http://wiki.swarma.net/index.php?title=%E5%BD%A9%E4%BA%91%E5%A4%A9%E6%B0%94API/v2 and wished to convert the JSONObject into printable Strings. However, when I am working on the following code, two errors occurred:

    public class getApi {
        private static final String WEATHER_MAP_URL = "https://api.caiyunapp.com/v2/TAkhjf8d1nlSlspN/121.6544,25.1552/realtime.json";
        private static final String WEATHER_TEST_API = "TAkhjf8d1nlSlspN";
    
        public static JSONObject getWeatherJson() {
            try {
                URL url = new URL( WEATHER_MAP_URL );
                HttpURLConnection connection =
                        (HttpURLConnection)url.openConnection();
    
                connection.addRequestProperty( "x-api-key", WEATHER_TEST_API );
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader( connection.getInputStream()) );
    
                StringBuffer json = new StringBuffer( 1024 );
                String tmp;
                while( (tmp = reader.readLine()) != null )
                    json.append(tmp).append("\n");
                reader.close();
    
                JSONObject data = new JSONObject( json.toString() );
                if(data.getJSONObject("status").toString() != "ok" ) {
                    return null;
                }
                return data;       
            }
            catch(Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        public static void main( String[] args ) {
            JSONObject WeatherJson = getWeatherJson();
            try {
                JSONArray details = WeatherJson.getJSONObject("result").getJSONObject("hourly").
                        getJSONArray("skycon");
                System.out.println(details.getJSONObject(0).getJSONObject("value").toString());
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    

    The JSONObject structure, which is also shown in the link above, is like this:

    {
        "status":"ok",
        "lang":"zh_CN", 
        "server_time":1443418212,
        "tzshift":28800, 
        "location":[
            25.1552, //latitude
            121.6544 //longitude
        ],
        "unit":"metric", 
        "result":{
            "status":"ok",
            "hourly":{ 
                "status":"ok",
                "skycon":[ 
                    {
                        "value":"Rain",
                        "datetime":"2015-09-28 13:00"
                    },
                    {
                     ...
                    }]
               }
          }
    }
    

    The error occurred:

    org.json.JSONException: JSONObject["status"] is not a JSONObject.
        at org.json.JSONObject.getJSONObject(JSONObject.java:557)
        at getApi.getWeatherJson(getApi.java:34)
        at getApi.main(getApi.java:45)
    Exception in thread "main" java.lang.NullPointerException
        at getApi.main(getApi.java:47)
    

    I have looked at similar posts on the topic is not a JSONObject Exception but found that none of them can help me. I suspect that something is wrong with requesting the data, so actually, getWeatherJson() returns a null object and results in the NullPointerException and JSONObjectException.

    Can anyone help me with the code?

  • Z.Richard
    Z.Richard almost 6 years
    Appreciate your help as well!