Shashi Ranjan
Shashi Ranjan

Reputation: 1561

Parsing InputStream into Json object and getting a value

The response json:

{
    "filename": "vcops-6.0.0-MPforAWS-1.1-1695068.pak",
    "links": [
        {
            "rel": "pak_information",
            "href": "https://<IP>:443/casa/upgrade/cluster/pak/MPforAWS-600/information"
        },
        {
            "rel": "pak_file_information",
            "href": "https://<IP>:443/casa/upgrade/slice/pak/MPforAWS-600/file_information"
        },
        {
            "rel": "pak_cluster_status",
            "href": "https://<IP>:443/casa/upgrade/cluster/pak/MPforAWS-600/status"
        }
    ],
    "pak_id": "MPforAWS-600"
}

I am using one helper of the framework we have. Framework returns response as "InputStream". I want to get "pak_id" from this "InputStream". I tried with inputStreamObj.toString() this does not work for me.

The method I am using is:

private String getPakId(InputStream uploadResponse) {
    String pakId = null;
    try {
        String responseString = readInputStream(uploadResponse);
        JSONObject jObj = new JSONObject(responseString);
        pakId = jObj.getString("pak_id").trim();
        Reporter.log("Pak id is=" + pakId, true);
    } catch (Exception e) {
        Reporter.log("Error in getting pak_id " + e.getMessage(), true);
    }
    return pakId;
}

and

private String readInputStream(InputStream inputStream) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(
            inputStream, "UTF-8"));
    String tmp;
    StringBuilder sb = new StringBuilder();
    while ((tmp = reader.readLine()) != null) {
        sb.append(tmp).append("\n");
    }
    if (sb.length() > 0 && sb.charAt(sb.length() - 1) == '\n') {
        sb.setLength(sb.length() - 1);
    }
    reader.close();
    return sb.toString();
}

Upvotes: 4

Views: 19881

Answers (3)

ender2101
ender2101

Reputation: 1

Well, better late than never... the following worked for me:

JSONTokener tokener = new JSONTokener(new InputStreamReader(istream));
JSONObject result;
try {
    result = new JSONObject(tokener);
} catch (JSONException e) {
    // Handle me!
}

Upvotes: -1

jeojavi
jeojavi

Reputation: 886

If you only want the pak_id value using only the standard library:

InputStream is = ...
String line;
StringBuilder text = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
while((line = reader.readLine()) != null) {
    text.append(line).append(" ");
}
String pakId = text.toString().replaceAll(".*\"pak_id\": \"([^\"]+)\".*", "$1");

Upvotes: 0

Arnout Engelen
Arnout Engelen

Reputation: 6907

If you look at the documentation for InputStream, you'll notice it will not promise you that toString will present you the contents of the stream.

If you're not interested in actually streaming the stream (which is reasonable if you expect the response to be small, like it appears to be the case here), it's OK to first get all the bytes from the stream, put them into a String, and then parse the String.

To get the String out of the InputStream, I'd recommend IOUtils.toString from apache commons-io.

Upvotes: 2

Related Questions