i'm writing code in java rest , output i'm getting in json format. want parse json string simple string in java i'm getting errors. below code:
package restapp; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.io.stringreader; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url; import java.util.*; import java.io.filenotfoundexception; import java.io.filereader; import java.io.ioexception; import org.json.jsonarray; import org.json.jsonexception; import java.util.iterator; import org.json.jsonobject; import org.json.simple.parser.jsonparser; import org.json.simple.parser.parseexception; public class testapp { public static void main(string[] args) throws jsonexception, parseexception { string output = "abc"; try { url url = new url("http://ip/sss-wsrcrest-controller-5.2.3.1/wsrcservice/wsrc/v1/processget?subsystemid=external&subsystempassword=password&operation=listsubscriptions&msisdn=1111"); httpurlconnection conn = (httpurlconnection) url.openconnection(); conn.setrequestmethod("get"); conn.setrequestproperty("accept", "application/json"); if (conn.getresponsecode() != 200) { throw new runtimeexception("failed : http error code : " + conn.getresponsecode()); } bufferedreader br = new bufferedreader(new inputstreamreader( (conn.getinputstream()))); //string output; system.out.println("output server .... \n"); while ((output = br.readline()) != null) { system.out.println(output); } conn.disconnect(); } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } jsonparser parser = new jsonparser(); //system.out.println(str); org.json.simple.jsonobject jsonobject = (org.json.simple.jsonobject) parser.parse(str); try { org.json.simple.jsonarray msg = (org.json.simple.jsonarray) jsonobject.get("keyparamarray"); int n = (msg).length(); (int = 0; < n; ++i) { jsonobject person = (msg).getjsonobject(i); system.out.println(person.getint("key")); } } catch (exception e) { e.printstacktrace(); } } }
the output
{ "errorcode": "0", "errormessage": "processed successfully", "keyparamarray": { "keyparam": [ { "key": "msisdn", "value": "123" }, { "key": "subscriberid", "value": "123" }, { "key": "customnumfield9", "value": "0" }, { "key": "customnumfield10", "value": "0" }, { "key": "customnumfield6", "value": "0" }, { "key": "customnumfield5", "value": "0" }, { "key": "customnumfield8", "value": "0" }, { "key": "customnumfield7", "value": "0" }, { "key": "customnumfield2", "value": "0" }, { "key": "customnumfield1", "value": "0" }, { "key": "customnumfield4", "value": "0" }, { "key": "customnumfield3", "value": "0" }, { "key": "parentsubscriberid", "value": "0" }, { "key": "active", "value": "1" }, { "key": "barringstatus", "value": "1" } ] } }
so want output
msisdn 123 subscriberid 123
... , on
org.json.simple.jsonobject jsonobject = (org.json.simple.jsonobject) parser.parse(str);
in above line of code, have passed str
parser.parse(str)
it's defined in class .
Comments
Post a Comment