我在JSON数组中的对象中的数组中获取字符串时遇到问题。
我的代码:
JSONArray arraydevice = new JSONArray(devicesJson);
JSONObject objh1 = arraydevice.getJSONObject(0);
System.out.println("The Json object for obj1 is: " + objh1);
JSONObject objh2 = arraydevice.getJSONObject(2);
System.out.println("The Json object for obj2 is: " + objh2);
String host1mac = objh1.getJSONArray("mac").getString(0);
String host2mac = objh2.getJSONArray("mac").getString(0);
System.out.println("The mac address of the host1 is " + host1mac);
System.out.println("The mac address of the host2 is " + host2mac);
控制台显示以下内容:
The Json object for obj1 is: {"lastSeen":1398513039112,"ipv4":[],"entityClass":"DefaultEntityClass","vlan":[],"mac":["00:00:00:00:00:01"],"attachmentPoint":[{"port":1,"errorStatus":null,"switchDPID":"00:00:00:00:00:00:00:01"}]}
The Json object for obj2 is: {"lastSeen":1398513039417,"ipv4":[],"entityClass":"DefaultEntityClass","vlan":[],"mac":["00:00:00:00:00:02"],"attachmentPoint":[{"port":1,"errorStatus":null,"switchDPID":"00:00:00:00:00:00:00:02"}]}
The mac address of the host1 is 00:00:00:00:00:01
Apr 26, 2014 3:26:37 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [PrintInfo] in context with path [/printinfo_rest] threw exception
java.lang.NullPointerException
at core.PrintInfo.doGet(PrintInfo.java:98)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
如控制台所示,服务器获取并打印第一个对象的mac地址(host1mac),但不为另一个对象(host2mac)执行此操作。这有什么问题?
提前致谢
答案 0 :(得分:0)
一个mac地址很可能是唯一的,将它变成一个json对象而不是一个数组会更合适。如果你不能改变它,那么你将如何迭代获取mac地址
JSONArray arraydevice = new JSONArray(devicesJson);
for(int i = 0 ; i < arraydevice.length(); i++){
String hostMac = arraydevice.getJSONObject(i)
.getJSONArray("mac")
.getString(0);
System.out.println("The mac address of the host" + i + "is: " + hostMac);
}