org.json.JSONObject $ 1类型的null null无法转换为JSONObject

Smi*_*Kmc 7 android json openweathermap

我在使用OpenWeatherMap API时遇到此异常错误.我只是想让结果成为一个JSONObject,但是null仍然会出现.

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

   }
Run Code Online (Sandbox Code Playgroud)

JSON数据很好,但我想要的是它成为一个JSONObject但是null部分正在捕获.任何想法为什么会发生这种情况?

同样来自网站的JSON Response是:

{"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],.....}
Run Code Online (Sandbox Code Playgroud)

为什么一开始没有null?谢谢您的帮助.

Gui*_*rré 6

在您收到的数据中,天气是JSONArray.

试试这个 :

 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){

}
Run Code Online (Sandbox Code Playgroud)

正如您所说,如果服务器返回的结果开始,null您将遇到此异常org.json.JSONException: Value null of type org.json.JSONObject$1 cannot be converted to JSONObject.

这是因为此结果不是有效的JSON内容.

如果您确实从服务器收到此无效内容,则解决方法可能是null在解析JSON之前删除它.

String crappyPrefix = "null";

if(result.startsWith(crappyPrefix)){
    result = result.substring(crappyPrefix.length(), result.length());
}
JSONObject jo = new JSONObject(result);
Run Code Online (Sandbox Code Playgroud)


Ami*_*mar 5

试试这个(对我有用)..我遇到了同样的问题

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;
        }
Run Code Online (Sandbox Code Playgroud)


mas*_*mjm 5

有时问题是您的响应为 null,但您期望一个 JSONObject。最好在服务器端解决它。如果您无法编辑服务器端代码,这个问题这个问题可能会有用