java - Converting BufferedReader to JSONObject or Map -
java - Converting BufferedReader to JSONObject or Map -
my code sends request server,
url obj = new url(url); httpurlconnection con = (httpurlconnection) obj.openconnection(); con.setrequestmethod("get"); con.setrequestproperty("user-agent", user_agent); bufferedreader in = new bufferedreader(new inputstreamreader(con.getinputstream())); i bufferedreader object prints,
{ "status": "error", "errorcode": "missing_params", "errormessage": null, "requestid": "20141014181739_11625805172", "downstreammoduleerrorcode": null, "object": [ "activity_code", "activity_name", "points", "frequency", "strategy", "vsa_app_access_token" ] } i want jsonobject or map this. tried converting string , manipulating it. it's not easy do. please help.
first string. can utilize custom librarys like
string message = org.apache.commons.io.ioutils.tostring(rd); or stringbuilder
stringbuilder sb = new stringbuilder(); string line; br = new bufferedreader(new inputstreamreader(is)); while ((line = br.readline()) != null) { sb.append(line); } then can parse it. since it's array (because of {) need create jsonobject.
jsonobject json = new jsonobject(sb.tostring()); then can access elements with
//{ "status": "error", "errorcode": "missing_params", "errormessage": null, "requestid": "20141014181739_11625805172", "downstreammoduleerrorcode": null, "object": [ "activity_code", "activity_name", "points", "frequency", "strategy", "vsa_app_access_token" ]} json.getstring("status"); or array with
jsonarray jsonarray = new jsonarray(json.getstring("object")); or utilize method getjsonarray()
jsonarray jsonarray = json.getjsonarray("object"); java json map httprequest
Comments
Post a Comment