首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >状态: JSONObject [“JSONObject”]不是状态

状态: JSONObject [“JSONObject”]不是状态
EN

Stack Overflow用户
提问于 2018-06-05 15:25:59
回答 2查看 10.3K关注 0票数 3

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

代码语言:javascript
复制
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结构,如下所示:

代码语言:javascript
复制
{
    "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"
                },
                {
                 ...
                }]
           }
      }
}

出现错误:

代码语言:javascript
复制
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中产生结果。

有人能帮我写代码吗?

EN

回答 2

Stack Overflow用户

发布于 2018-06-05 15:43:03

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

票数 4
EN

Stack Overflow用户

发布于 2018-06-05 15:36:34

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

代码语言:javascript
复制
JSONObject data = new JSONObject(json.toString());
  if(data.getJSONObject("result").get("status").toString() != "ok" ) {
    return null;
  }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50694404

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档