【问题标题】:org.json.JSONException: JSONObject["status"] is not a JSONObjectorg.json.JSONException: JSONObject["status"] 不是 JSONObject
【发布时间】:2018-11-14 14:31:08
【问题描述】:

我现在正在使用来自http://wiki.swarma.net/index.php?title=%E5%BD%A9%E4%BA%91%E5%A4%A9%E6%B0%94API/v2 的天气 API,并希望将 JSONObject 转换为可打印的字符串。但是,当我处理以下代码时,出现了两个错误:

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

上面链接中也显示的JSONObject结构是这样的:

{
    "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"
                },
                {
                 ...
                }]
           }
      }
}

发生错误:

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)

我查看了关于主题 is not a JSONObject Exception 的类似帖子,但发现没有一个可以帮助我。我怀疑请求数据有问题,所以实际上,getWeatherJson() 返回一个空对象并导致NullPointerExceptionJSONObjectException

谁能帮我写代码?

【问题讨论】:

  • 你使用了哪个 JSON Parser?
  • 因为 status 真的不是 JSON 对象——它是一个简单的字符串。你应该做一个getString
  • @Vasan 感谢您的帮助。它有效。
  • @DhanasekaranDon 对不起,我不知道...我是 JSON 新手

标签: java json httpurlconnection


【解决方案1】:

根据getJSONObject() Javadoc,如果返回的对象不是真正的JSON对象,则此方法将抛出异常,这不是因为“状态”是一个字符串。因此,请尝试使用data.getString("status")

【讨论】:

    【解决方案2】:

    您发布的 JSON 文档中的状态字段不是对象。在 JSON 中,对象用{} 括号括起来。然而,result 节点是一个嵌套对象,其中包含状态键/值对。请尝试以下操作:

    JSONObject data = new JSONObject(json.toString());
      if(data.getJSONObject("result").get("status").toString() != "ok" ) {
        return null;
      }
    

    【讨论】:

    • 也感谢您的帮助!
    猜你喜欢
    • 2018-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-21
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    • 2017-01-03
    相关资源
    最近更新 更多