Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value null of type org.json.JSONObject$1 cannot be converted to JSONObject

I'm getting this exception error when using the OpenWeatherMap API. I'm just trying to get the result to be an JSONObject, but null keeps coming up.

@Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        // What's coming in as result...
        // Printed to the console...
        // null{"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":800,"main":"Clear",
        // "description":"clear sky","icon":"01d"}],...}


        try {
            JSONObject jsonObject = new JSONObject(result);
            String weatherInfo = jsonObject.getString("weather");

            Log.i("Weather Info", weatherInfo);
        } catch (JSONException e) {
            e.printStackTrace();
        }

   }

The JSON data comes in fine but all I want is it to become a JSONObject but the null part is catching. Any Ideas why that might be happening?

Also from the site the JSON Response coming in is:

{"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],.....}

How come that doesn't have null at the start? Thank you for your help.

like image 305
SmiffyKmc Avatar asked Apr 13 '16 13:04

SmiffyKmc


People also ask

Are null values allowed in JSON?

Null values JSON has a special value called null which can be set on any type of data including arrays, objects, number and boolean types.

How do I check if a JSON key is null?

Description. JsonObject::containsKey() tests whether a key exists in the object pointed by the JsonObject . If the JsonObject is null, this function returns false .


2 Answers

In the data you receive weather is a JSONArray.

Try this :

 String json = "{\"coord\":{\"lon\":-0.13,\"lat\":51.51},\"weather\":[{\"id\":800,\"main\":\"Clear\",\"description\":\"clear sky\",\"icon\":\"01d\"}],.....}";

try{
    JSONObject jo = new JSONObject(json);
    JSONArray weather = jo.getJSONArray("weather");
    for(int i = 0;i < weather.length(); i++){
        JSONObject w = weather.getJSONObject(i);
        String main = w.getString("main");
        String description = w.getString("description");
        //...
    }
}catch (Exception e){

}

As you said if the result returned by the server start with null you will have this exception org.json.JSONException: Value null of type org.json.JSONObject$1 cannot be converted to JSONObject.

This is because this result is not a valid JSON content.

If you really receive this invalid content from the server a workaround can be to remove the null before parsing the JSON.

String crappyPrefix = "null";

if(result.startsWith(crappyPrefix)){
    result = result.substring(crappyPrefix.length(), result.length());
}
JSONObject jo = new JSONObject(result);
like image 102
Guillaume Barré Avatar answered Nov 01 '22 23:11

Guillaume Barré


Try This (worked for me).. I was having same problem

public class DownloadData extends AsyncTask<String , Void, String >{

        HttpURLConnection httpURLConnection =null;
        URL url;


String resultString="";  <------- instead of setting it to null


        @Override
        protected String doInBackground(String... urls) {


            try {
                url = new URL(urls[0]);
                httpURLConnection = (HttpURLConnection) url.openConnection();
                InputStream is = httpURLConnection.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                int data = isr.read();
                while(data != -1){
                    char ch = (char) data;
                    resultString += ch;
                    data = isr.read();

                }
                return resultString;



            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


            return null;
        }
like image 29
Amit Kumar Avatar answered Nov 02 '22 01:11

Amit Kumar