try {
URL url = new URL("http://tmi.twitch.tv/group/user/m0e_tv/chatters");
URLConnection conn = url.openConnection();
BufferedReader br = new BufferedReader( new InputStreamReader( conn.getInputStream() ));
String inputLine = br.readLine();
br.close();
JsonObject jsonObj = JsonObject.readFrom(inputLine); //AT THIS POINT I GET ERROR
....
} catch (IOException e)
{
e.printStackTrace();
}
Anything wrong with that API or with my coding plz help
edit: Oh this is the error I get: Exception in thread "main" com.eclipsesource.json.ParseException: Unexpected end of input at 1:0
edit2: I believe there is nothing wrong with my code because I am making a call to https://api.twitch.tv/kraken/streams/m0e_tv at another place and that works like a charm. That’s why I don’t think its a programming error. And I also checked online that that link returns a JSON. I checked it at http://jsonlint.com/. So I don’t know what could be wrong
that is so insane. I am using bufferedreader and that class doesn’t have a method that reads the whole thing. And it doesn’t even have a method that checks if it has more lines (something like hasNext(…) …) so I can run a loop and store all of it. What should I do? Plz help!
Ty MR 3ventic for the help. This is what I did: try { URL url = new URL("https://tmi.twitch.tv/group/user/m0e_tv/chatters"); URLConnection conn = url.openConnection(); BufferedReader br = new BufferedReader( new InputStreamReader( conn.getInputStream() )); String line; String all = ""; while ((line = br.readLine()) != null) { all = all + line; } br.close(); JsonObject jsonObj = JsonObject.readFrom(all); //works good now System.err.println(jsonObj.get("chatters")); } catch (IOException e) { e.printStackTrace(); }
I can get the chatters property fine. But I want viewers property inside that chatters property. How do I get that? I tried jsonObj.get("chatters.viewers") and jsonObj.get("chatters[viewers]") How do I access that property please help Thank you
very very very easy. I know what to do. This is what I did:
JsonObject jsonObj = JsonObject.readFrom(all); JsonObject jsonObj2 = (JsonObject) jsonObj.get("chatters"); System.err.println(jsonObj2.get("viewers")); //This prints out all the viewers //and only the viewers and not //garbage like mods etc...