Exception while trying to get value from a json object in java webservice -
when tried value json object in java webservice side getting runtime exceptions,please find exception getting below:
when tried value json object in java webservice side getting runtime exceptions, please find json object below:
{"data":[{"userid":657,"name":"eliina","username":"admin","aim_name":" eliina ","aim_title":"administrator","password":"eli456456","role":1,"rolename":"client ain","clientno":"540","id":8,"client_name":"eliina","defaulturl":"clientsettings.htm","attempts":0,"aimaccess":1}]}
please find java method wrote:
public void insertlist(string ip, jsonobject loginresult) { try{ string n = loginresult.getstring("data"); } catch( jsonexception je ) { je.printstacktrace(); } }
please find exception getting below:
30-sep-2015 10:27:11.007 info [http-nio-8084-exec-10] org.apache.catalina.startup.hostconfig.deploydescriptor deployment of configuration descriptor /home/ty02/.netbeans/8.0.2/apache-tomcat-8.0.15.0_base/conf/catalina/localhost/mobileservice.xml has finished in 2,217 ms org.codehaus.jettison.json.jsonexception: jsonobject["username"] not found. @ org.codehaus.jettison.json.jsonobject.get(jsonobject.java:360) @ org.codehaus.jettison.json.jsonobject.getstring(jsonobject.java:487) @ com.zimmer.mobileservice.resources.login.insertauditlist(login.java:84) @ com.zimmer.mobileservice.resources.login.checklogin(login.java:71) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:606) @ com.sun.jersey.spi.container.javamethodinvokerfactory$1.invoke(javamethodinvokerfactory.java:60) @ com.sun.jersey.server.impl.model.method.dispatch.abstractresourcemethoddispatchprovider$typeoutinvoker._dispatch(abstractresourcemethoddispatchprovider.java:185) @ com.sun.jersey.server.impl.model.method.dispatch.resourcejavamethoddispatcher.dispatch(resourcejavamethoddispatcher.java:75) @ com.sun.jersey.server.impl.uri.rules.httpmethodrule.accept(httpmethodrule.java:288) @ com.sun.jersey.server.impl.uri.rules.resourceclassrule.accept(resourceclassrule.java:108) @ com.sun.jersey.server.impl.uri.rules.righthandpathrule.accept(righthandpathrule.java:147) @ com.sun.jersey.server.impl.uri.rules.rootresourceclassesrule.accept(rootresourceclassesrule.java:84) @ com.sun.jersey.server.impl.application.webapplicationimpl._handlerequest(webapplicationimpl.java:1469) @ com.sun.jersey.server.impl.application.webapplicationimpl._handlerequest(webapplicationimpl.java:1400) @ com.sun.jersey.server.impl.application.webapplicationimpl.handlerequest(webapplicationimpl.java:1349) @ com.sun.jersey.server.impl.application.webapplicationimpl.handlerequest(webapplicationimpl.java:1339) @ com.sun.jersey.spi.container.servlet.webcomponent.service(webcomponent.java:416) @ com.sun.jersey.spi.container.servlet.servletcontainer.service(servletcontainer.java:537) @ com.sun.jersey.spi.container.servlet.servletcontainer.service(servletcontainer.java:708) @ javax.servlet.http.httpservlet.service(httpservlet.java:725) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:291) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:206) @ org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:52) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:239) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:206) @ org.netbeans.modules.web.monitor.server.monitorfilter.dofilter(monitorfilter.java:393) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:239) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:206) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:219) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:106) @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:501) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:142) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:79) @ org.apache.catalina.valves.abstractaccesslogvalve.invoke(abstractaccesslogvalve.java:610) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:88) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:537) @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1085) @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:658) @ org.apache.coyote.http11.http11nioprotocol$http11connectionhandler.process(http11nioprotocol.java:222) @ org.apache.tomcat.util.net.nioendpoint$socketprocessor.dorun(nioendpoint.java:1556) @ org.apache.tomcat.util.net.nioendpoint$socketprocessor.run(nioendpoint.java:1513) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1145) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:615) @ org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61) @ java.lang.thread.run(thread.java:745)
your input json is,
{"data":[{"userid":657,"name":"eliina","username":"admin","aim_name":" eliina ","aim_title":"administrator","password":"eli456456","role":1,"rolename":"client ain","clientno":"540","id":8,"client_name":"eliina","defaulturl":"clientsettings.htm","attempts":0,"aimaccess":1}]}
so means key data,
value jsonarray
, trying jsonarray string
facing such exception. need is, need value jsonarray like,
jsonarray value = loginresult.getjsonarray("data");
edited answer:
now value jsonarray contains array object can key
like,
jsonarray value = loginresult.getjsonarray("data"); string userid = ((jsonobject) value.get(0)).getstring("userid");
better check haskey check
, array size check
avoid nasty exceptions.
Comments
Post a Comment