Sunday, August 21, 2016

Convert InputStream to JSONObject

/**
     * JSON object input stream.
     *
     * @param in
     *            the in
     * @return the JSON object
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static JSONObject jsonObjectInputStream(InputStream in)
            throws IOException {
        String line;
        BufferedReader br = new BufferedReader(new InputStreamReader(in,
                Charset.forName("UTF-8")));
        JSONObject json = new JSONObject();
        while ((line = br.readLine()) != null) {
            try {
                json = new JSONObject(line);
            } catch (JSONException e) {
                return null;
            }
        }
        return json;
    }

No comments:

Post a Comment