简体   繁体   中英

How to parse JSON sections using json-simple in Java?

I'm currently using the Twitch API to get information about a livestream. However, I can't seem to get the data under the stream section for viewers and such. I'm using json-simple as a maven dependency.

{
  "_links": {
"channel": "https://api.twitch.tv/kraken/channels/test_channel",
"self": "https://api.twitch.tv/kraken/streams/test_channel"},
"stream": {
"game": "StarCraft II: Heart of the Swarm",
"viewers": 2123,
"average_fps": 29.9880749574,
"delay": 0,
"video_height": 720,
"is_playlist": false,
"created_at": "2015-02-12T04:42:31Z",
"_id": 4989654544,

Here's an example for getting the viewers data:

JSONParser parser = new JSONParser();
// handle exceptions for this line
JSONObject jsonObject = (JSONObject) parser.parse(new FileReader(new File("data.json")));
JSONObject streamObject = (JSONObject) jsonObject.get("stream");
long viewers = (Long) streamObject.get("viewers");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM