java - 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 using 1 helper of framework have. framework returns response "inputstream". want "pak_id" "inputstream". tried inputstreamobj.tostring() not work me.

the method 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(); } 

if @ documentation inputstream, you'll notice not promise tostring present contents of stream.

if you're not interested in streaming stream (which reasonable if expect response small, appears case here), it's ok first bytes stream, put them string, , parse string.

to string out of inputstream, i'd recommend ioutils.tostring apache commons-io.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -