获取 Json 条信息

Getting a Json information

我在 Java 中编码,并且我正在使用 minimal-json 库。我正在尝试从 json 文本中获取一些信息(如果它是一个数组,则为 idk)。

我正在尝试访问 "stream" 中的 "game" 值,但我总是因 nullpointer 或 parseexception 而崩溃。

这是我要获取的 json 字符串(来自 Twitch Api):

{"_links":{"self":"https://api.twitch.tv/kraken/streams/hackerc0w","channel":"https://api.twitch.tv/kraken/channels/hackerc0w"},"stream":{"_id":13817896816,"game":"Programming","viewers":13,"created_at":"2015-04-01T13:54:54Z","video_height":1080,"average_fps":59.9235368156,"_links":{"self":"https://api.twitch.tv/kraken/streams/hackerc0w"},"preview":{"small":"http://static-cdn.jtvnw.net/previews-ttv/live_user_hackerc0w-80x45.jpg","medium":"http://static-cdn.jtvnw.net/previews-ttv/live_user_hackerc0w-320x180.jpg","large":"http://static-cdn.jtvnw.net/previews-ttv/live_user_hackerc0w-640x360.jpg","template":"http://static-cdn.jtvnw.net/previews-ttv/live_user_hackerc0w-{width}x{height}.jpg"},"channel":{"_links":{"self":"https://api.twitch.tv/kraken/channels/hackerc0w","follows":"https://api.twitch.tv/kraken/channels/hackerc0w/follows","commercial":"https://api.twitch.tv/kraken/channels/hackerc0w/commercial","stream_key":"https://api.twitch.tv/kraken/channels/hackerc0w/stream_key","chat":"https://api.twitch.tv/kraken/chat/hackerc0w","features":"https://api.twitch.tv/kraken/channels/hackerc0w/features","subscriptions":"https://api.twitch.tv/kraken/channels/hackerc0w/subscriptions","editors":"https://api.twitch.tv/kraken/channels/hackerc0w/editors","videos":"https://api.twitch.tv/kraken/channels/hackerc0w/videos","teams":"https://api.twitch.tv/kraken/channels/hackerc0w/teams"},"background":null,"banner":null,"broadcaster_language":"en","display_name":"hackerc0w","game":"Programming","logo":null,"mature":false,"status":"Coding a Chatbot in C","partner":false,"url":"http://www.twitch.tv/hackerc0w","video_banner":null,"_id":41236491,"name":"hackerc0w","created_at":"2013-03-11T17:08:22Z","updated_at":"2015-04-01T17:17:44Z","delay":0,"followers":28,"profile_banner":null,"profile_banner_background_color":null,"views":2948,"language":"de"}}}

直播时我得到的方式是这样的:

public static boolean isStreamLive(String channel) { try { URL url = new URL(TWITCH_STREAM.replace("$c$", channel)) ); URLConnection conn = url.openConnection(); BufferedReader br = new BufferedReader( new InputStreamReader( conn.getInputStream() )); String inputLine = br.readLine(); br.close(); JsonObject jsonObj = JsonObject.readFrom(inputLine); return ( jsonObj.get("stream").isNull() )?false:true; } catch (IOException e) { return false; } }

而且我主要是尝试复制上面的方法。但是我一直在查看库的文档才知道,并且我一直在尝试很多东西。
最后是这样的:

public static String checkGame(String channel) { if (!isStreamLive(channel)) return "El Estreamer esta Offline!"; try { URL url = new URL( insertChannel(TWITCH_STREAM, channel) ); URLConnection conn = url.openConnection(); BufferedReader br = new BufferedReader( new InputStreamReader( conn.getInputStream() )); String inputLine = br.readLine(); br.close(); JsonObject object = JsonObject.readFrom(inputLine); JsonValue value = object.get("stream").asObject(); String msg = value.valueOf("status").toString(); return msg; } catch (IOException e) { return "Algo raro paso :/ error: 3"; } return channel; }

您所展示的代码存在两个主要问题。

您遇到的第一个问题是您试图从错误的嵌套对象中获取状态。您正在从原始 json 获取 stream 对象,然后尝试从中获取 status 键的值,但是在 json 中您发布了 status 是嵌套 channel 对象的一部分。

json对象的结构是这样的:

{
    "_links": { ... },
    "stream": {
        ...
        "channel":{
            "_links": { ...  },
            ...
            "status":"Coding a Chatbot in C",
            ...
        }
    }
}

...所以你不能直接从stream得到status,你需要从stream得到channel然后得到status来自 那个.


第二个问题是您试图使用 valueOf()JsonObject 中提取一个值。 valueOf() 是一个 static 方法,它根据提供的输入创建一个新对象,因此它实际上并不使用您调用它的对象中的数据。

调用 value.valueOf("status") 将完全忽略 value 中的数据并创建一个包含字符串 "status" 的新 JsonObject

如果要获取嵌套对象的值,需要通过一系列连续的get("objName").asObject()调用向下钻取,然后调用.get("key")获取您想要的值:

    // hardcoded for example, actually would be read from BufferedReader
    String inputLine = "{\"_links\":{\"self\":\"https://api.twitch.tv/kraken/streams/hackerc0w\",\"channel\":\"https://api.twitch.tv/kraken/channels/hackerc0w\"},\"stream\":{\"_id\":13817896816,\"game\":\"Programming\",\"viewers\":13,\"created_at\":\"2015-04-01T13:54:54Z\",\"video_height\":1080,\"average_fps\":59.9235368156,\"_links\":{\"self\":\"https://api.twitch.tv/kraken/streams/hackerc0w\"},\"preview\":{\"small\":\"http://static-cdn.jtvnw.net/previews-ttv/live_user_hackerc0w-80x45.jpg\",\"medium\":\"http://static-cdn.jtvnw.net/previews-ttv/live_user_hackerc0w-320x180.jpg\",\"large\":\"http://static-cdn.jtvnw.net/previews-ttv/live_user_hackerc0w-640x360.jpg\",\"template\":\"http://static-cdn.jtvnw.net/previews-ttv/live_user_hackerc0w-{width}x{height}.jpg\"},\"channel\":{\"_links\":{\"self\":\"https://api.twitch.tv/kraken/channels/hackerc0w\",\"follows\":\"https://api.twitch.tv/kraken/channels/hackerc0w/follows\",\"commercial\":\"https://api.twitch.tv/kraken/channels/hackerc0w/commercial\",\"stream_key\":\"https://api.twitch.tv/kraken/channels/hackerc0w/stream_key\",\"chat\":\"https://api.twitch.tv/kraken/chat/hackerc0w\",\"features\":\"https://api.twitch.tv/kraken/channels/hackerc0w/features\",\"subscriptions\":\"https://api.twitch.tv/kraken/channels/hackerc0w/subscriptions\",\"editors\":\"https://api.twitch.tv/kraken/channels/hackerc0w/editors\",\"videos\":\"https://api.twitch.tv/kraken/channels/hackerc0w/videos\",\"teams\":\"https://api.twitch.tv/kraken/channels/hackerc0w/teams\"},\"background\":null,\"banner\":null,\"broadcaster_language\":\"en\",\"display_name\":\"hackerc0w\",\"game\":\"Programming\",\"logo\":null,\"mature\":false,\"status\":\"Coding a Chatbot in C\",\"partner\":false,\"url\":\"http://www.twitch.tv/hackerc0w\",\"video_banner\":null,\"_id\":41236491,\"name\":\"hackerc0w\",\"created_at\":\"2013-03-11T17:08:22Z\",\"updated_at\":\"2015-04-01T17:17:44Z\",\"delay\":0,\"followers\":28,\"profile_banner\":null,\"profile_banner_background_color\":null,\"views\":2948,\"language\":\"de\"}}}";

    JsonObject object = JsonObject.readFrom(inputLine);    // parse json into object
    JsonObject stream = object.get("stream").asObject();   // get "stream" sub-object
    JsonObject channel = stream.get("channel").asObject(); // get "channel" sub-object
    JsonValue status = channel.get("status");              // get the value of "status"
    String msg = status.asString();
    System.out.println(msg);  // Coding a Chatbot in C