Java read JSON input stream
Wrap it with a BufferedReader
and start reading the data from it:
StringBuilder sb = new StringBuilder();try (BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()))) { String line; while ( (line = br.readLine()) != null) { sb.append(line).append(System.lineSeparator()); } String content = sb.toString(); //as example, you can see the content in console output System.out.println(content);}
Once you have it as a String, parse it with a library like Gson or Jackson.
StringBuffer buffer = new StringBuffer(); int ch; boolean run = true; try { while(run) { ch = reader.read(); if(ch == -1) { break; } buffer.append((char) ch); if(isJSONValid(buffer.toString())){ run = false;} } } catch (SocketTimeoutException e) { //handle exception }private boolean isJSONValid(String test) { try { new JSONObject(test); } catch (JSONException ex) { try { new JSONArray(test); } catch (JSONException ex1) { return false; } } return true; }