从另一个 JSONObject 创建子 JSONObject 时出错

Error while creating a sub JSONObject from another JSONObject

我正在开发一个 Android 应用程序,其功能之一是使用 Twitch API 了解主播何时进行流式传输。 当流媒体正在流式传输时,如果我连接到 https://api.twitch.tv/kraken/streams/,我会得到一个字符串,我用它来构建一个 JSON 对象,如下所示:

{  
   "_links":{  
      "self":"https://api.twitch.tv/kraken/streams/srkevo1",
      "channel":"https://api.twitch.tv/kraken/channels/srkevo1"
   },
   "stream":{  
      "_id":15361851552,
      "game":"Super Smash Bros. for Wii U",
      "viewers":42613,
      "created_at":"2015-07-18T15:07:59Z",
      "video_height":720,
      "average_fps":59.319897084,
      "_links":{  
         "self":"https://api.twitch.tv/kraken/streams/srkevo1"
      },
      "preview":{  
         "small":"http://static-cdn.jtvnw.net/previews-ttv/live_user_srkevo1-80x45.jpg",
         "medium":"http://static-cdn.jtvnw.net/previews-ttv/live_user_srkevo1-320x180.jpg",
         "large":"http://static-cdn.jtvnw.net/previews-ttv/live_user_srkevo1-640x360.jpg",
         "template":"http://static-cdn.jtvnw.net/previews-ttv/live_user_srkevo1-{width}x{height}.jpg"
      },
      "channel":{  
         "_links":{  
            "self":"https://api.twitch.tv/kraken/channels/srkevo1",
            "follows":"https://api.twitch.tv/kraken/channels/srkevo1/follows",
            "commercial":"https://api.twitch.tv/kraken/channels/srkevo1/commercial",
            "stream_key":"https://api.twitch.tv/kraken/channels/srkevo1/stream_key",
            "chat":"https://api.twitch.tv/kraken/chat/srkevo1",
            "features":"https://api.twitch.tv/kraken/channels/srkevo1/features",
            "subscriptions":"https://api.twitch.tv/kraken/channels/srkevo1/subscriptions",
            "editors":"https://api.twitch.tv/kraken/channels/srkevo1/editors",
            "videos":"https://api.twitch.tv/kraken/channels/srkevo1/videos",
            "teams":"https://api.twitch.tv/kraken/channels/srkevo1/teams"
         },
         "background":null,
         "banner":null,
         "broadcaster_language":"en",
         "display_name":"srkevo1",
         "game":"Super Smash Bros. for Wii U",
         "logo":"http://static-cdn.jtvnw.net/jtv_user_pictures/srkevo1-profile_image-e46c53476d9b74c7-300x300.png",
         "mature":null,
         "status":"Evolution 2015 - Main Stage (July 17-19) all brackets http://evo2015.s3.amazonaws.com/brackets/index.html",
         "partner":true,
         "url":"http://www.twitch.tv/srkevo1",
         "video_banner":"http://static-cdn.jtvnw.net/jtv_user_pictures/srkevo1-channel_offline_image-ee2fc39d6ebb7735-640x360.jpeg",
         "_id":30917811,
         "name":"srkevo1",
         "created_at":"2012-05-30T16:57:11Z",
         "updated_at":"2015-07-18T16:18:40Z",
         "delay":0,
         "followers":82134,
         "profile_banner":null,
         "profile_banner_background_color":null,
         "views":20938144,
         "language":"en"
      }
   }
}

这就是我获取 JSON 对象的方式:

public class GetStreamStatus extends AsyncTask<Void,Void,String> {
        @Override
        protected String doInBackground(Void... params) {
            String res = bibixChannel.getJson("streams");
            return res;
        }

        @Override
        protected void onPostExecute(String s) {
            channelStatusString = s;
            channelStatusObject = bibixChannel.buildJSON(s);
        }
    }

buildJson() 方法很简单:

protected JSONObject buildJSON(String jsonRaw){
        JSONObject json = null;
        try{
            json = new JSONObject(jsonRaw);
        }catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }

但是之后,要知道主播是否正在播放,JSON 字符串的一部分会像这样被清空: 如果主播正在直播,你会得到拳头 JSON 我写在上面 post,否则你会得到类似的东西 :

{  
   "_links":{  
      "self":"https://api.twitch.tv/kraken/streams/bibixhd",
      "channel":"https://api.twitch.tv/kraken/channels/bibixhd"
   },
   "stream":null
}

我想要做的是获取另一个实例变量中的 "stream" 部分,以恢复有关流的信息或显示离线消息。

我从你的 OP 那里得到的是 channelStatusObject return 你的 JSONObject 包含 stream 键。现在你要检查的是这个stream是否是null。所以你可以做类似的事情:-

protected JSONObject checkStream(JSONObject parentObject){
        JSONObject json = (JSONObject) parentObject.get("stream");
        return json; // will return null if stream is null else stream JSONObject
    }

您可以使用此方法并检查其 return 值以查看 stream 是否为 null。