0

When making a http request to get a list of users in a twitch chat, I get a response like this:

{
  "_links": {},
  "chatter_count": 27,
  "chatters": {
    "moderators": [
      "user1",
      "user2",
      "user3",
      "...",
    ],
    "viewers": [
      "user4",
      "user5",
      "user6",
      "...",
    ]
  }
}

How can I get all this in one String? So I have a String looking like this:

{ "_links": {}, "chatter_count": 27, "chatters": { "moderators": [ "user1", "user2", "user3", "...", ], "viewers": [ "user4", "user5", "user6", "...", ] } }

My request:

    URL url = new URL("http://tmi.twitch.tv/group/user/" + streamerName + "/chatters");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();       
    conn.setRequestMethod("GET");
    conn.addRequestProperty("Accept", "application/vnd.twitchtv.v5+json");
    conn.addRequestProperty("Client-Id", "j113orc4zqptuzyqnnw1vfel33esj0");       
    BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));

    while (null != (temp = br.readLine())) { 
        System.out.println(temp);
    }
devistud
  • 3
  • 4
  • @Andremoniy it seems OP is simply reading from a `BufferedReader`, at least to me, although there is not much context. – Salem Feb 07 '17 at 18:22
  • http://stackoverflow.com/questions/12173416/how-do-i-get-the-compact-form-of-pretty-printed-json-code – Andremoniy Feb 07 '17 at 18:27

1 Answers1

0

You could replace all newlines with spaces. Includes \r possibility for Windows-style newlines.

String jsonOneLine = json.replaceAll("(\r)?\n", " ");

Although it is possible that your way of printing the JSON is resulting in the newlines. What libraries are you using? If there is any equivalent of a prettyPrint method, don't use it if you want the JSON to not be reformatted.

For example, use the raw results instead of an already-parsed JSON object.

I am not sure how you are reading the request results, but:

StringBuilder b = new StringBuilder();
int read = reader.read();
while(read != -1) {
    b.append((char) read);
    read = reader.read();
}

If you are sure the request does contain newlines, you can modify this reading method to ignore them.


Edit:

Well, since you are using readLine, why not ignore newlines?

for(String temp = br.readLine(); temp != null; temp = br.readLine())
    System.out.print(temp + ' '); // <-- NOT println!

Or, if you want to keep the JSON:

StringBuilder progress = new StringBuilder();
for(String temp = br.readLine(); temp != null; temp = br.readLine())
    progress.append(' ').append(temp)
String oneLine = progress.length() < 1 ? "{}" //No JSON found (SB empty)
                                       : progress.substring(1).toString();

Substring it to ignore the first space.

Salem
  • 13,516
  • 4
  • 51
  • 70
  • I kinda know what you want me to do but I dont really know how: "You could replace all newlines with spaces." easy to understand. The problem is, that I'm not getting the JSON as a whole, I'm getting it line by line. (I edited my question at the end so you can see how I'm printing it.) @1blustone – devistud Feb 07 '17 at 21:05
  • @devistud Then use `read` instead of `readLine` :) my answer includes a simple version of using that – Salem Feb 08 '17 at 08:06