簡體   English   中英

將JSON字符串解析為Java的問題

[英]Issues with parsing JSON String to Java

我在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)進行打印。 這是什么問題?

提前致謝

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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM