Reputation: 93
When i tried to get the value from my json object in my java webservice side i am getting some runtime exceptions,PLease find the exception i am getting below:
When i tried to get the value from my json object in my java webservice side i am getting some runtime exceptions,
Please find my 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 the java method i wrote:
public void insertList(String ip, JSONObject loginresult)
{
try{
String n = loginresult.getString("data");
} catch( JSONException je ) {
je.printStackTrace();
}
}
PLease find the exception i am 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.
at org.codehaus.jettison.json.JSONObject.get(JSONObject.java:360)
at org.codehaus.jettison.json.JSONObject.getString(JSONObject.java:487)
at com.zimmer.mobileservice.resources.Login.insertauditList(Login.java:84)
at com.zimmer.mobileservice.resources.Login.checkLogin(Login.java:71)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1556)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1513)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Upvotes: 0
Views: 4139
Reputation: 887
This could be most generalized way of doing,
ObjectMapper mapper = new ObjectMapper();
user = mapper.readValue(loginresult.toString(), User.class);
user is a POJO class which has all getter setter fields that are there in your input.
Upvotes: 0
Reputation: 3463
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 which means for key data,
value is the JSONArray
, but you are trying to get JSONArray as String
so you are facing such exception. What you need to do is, need to get the value as JSONArray like,
JSONArray value = loginresult.getJSONArray("data");
Edited Answer:
Now the value JSONArray contains the array object from which you can get for a key
like,
JSONArray value = loginresult.getJSONArray("data");
String userId = ((JSONObject) value.get(0)).getString("userid");
Better to check for hasKey check
and array size check
to avoid nasty exceptions.
Upvotes: 1
Reputation: 487
The "data" object you trying get is array in json. you will get value as below.
jsonObject.getJSONArray("data")
Upvotes: 0