如何解析Java中的JSONArray中的JSONObjects?

问题描述:

我试图从Floodlight(一种SDN控制器)REST API读取数据,并将其输入到其他软件的REST API中。我有这个从泛光灯的REST API写着:如何解析Java中的JSONArray中的JSONObjects?

private JSONArray getFlData(String path) { 
    try { 
     logger.info("getData URL: " + path); 
     URL url = new URL("http://localhost:8080" + path); 
     HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
     conn.setDoInput(true); 
     conn.setRequestMethod("GET"); 

     BufferedReader br = new BufferedReader(
      new InputStreamReader(conn.getInputStream())); 

     String inputline; 
     StringBuffer response = new StringBuffer(); 
     while((inputline = br.readLine()) != null) { 
      response.append(inputline); 
     } 
     JSONArray jsonr = new JSONArray(response.toString()); 
     br.close(); 
     conn.disconnect(); 
     return jsonr; 
    } 
    catch (MalformedURLException e) { 
     logger.info("Bad URL (getData) " + path); 
    } 
    catch (IOException e) { 
     logger.info("IOException (getData)" + path); 
    } 
    catch (JSONException e) { 
     logger.info("Bad JSON (getData)" + path); 
    } 
    return null; 
} 

然后我解析这些信息纳入名单:

JSONArray flswitches = getFlData("/wm/core/controller/switches/json"); 
    List<String> switchDPIDlist = new ArrayList<String>(); 
    List<String> switchIPlist = new ArrayList<String>(); 
    for (int i = 0;i < flswitches.length();i++){ 
     try { 
      switchDPIDlist.add(flswitches.getJSONObject(i).getString("switchDPID")); 
      switchIPlist.add(flswitches.getJSONObject(i).getString("inetAddress")); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 

其中一期工程。但是,当我尝试对网络中的主机输出做同样的事情时,我遇到了问题。作为参考,这里的卷曲输出简单的事情,我可以做到这一点:

curl http://127.0.0.1:8080/wm/core/controller/switches/json 
[{"inetAddress":"/127.0.0.1:43663","connectedSince":1456305460978,"switchDPID":"00:00:00:00:00:00:00:01"},{"inetAddress":"/127.0.0.1:43664","connectedSince":1456305460981,"switchDPID":"00:00:00:00:00:00:00:03"},{"inetAddress":"/127.0.0.1:43665","connectedSince":1456305460984,"switchDPID":"00:00:00:00:00:00:00:02"}] 

但是,当我试图像这样的更加复杂的输出使用它,我碰到的问题:

curl http://127.0.0.1:8080/wm/device/ 
[{"entityClass":"DefaultEntityClass","mac":["86:2b:a2:f1:2b:9c"],"ipv4":["10.0.0.1"],"ipv6":[],"vlan":["0x0"],"attachmentPoint":[{"switchDPID":"00:00:00:00:00:00:00:02","port":1,"errorStatus":null}],"lastSeen":1456312407529},{"entityClass":"DefaultEntityClass","mac":["1e:94:63:67:1e:d1"],"ipv4":["10.0.0.3"],"ipv6":[],"vlan":["0x0"],"attachmentPoint":[{"switchDPID":"00:00:00:00:00:00:00:03","port":1,"errorStatus":null}],"lastSeen":1456312407625},{"entityClass":"DefaultEntityClass","mac":["06:d7:e0:c5:60:86"],"ipv4":["10.0.0.2"],"ipv6":[],"vlan":["0x0"],"attachmentPoint":[{"switchDPID":"00:00:00:00:00:00:00:02","port":2,"errorStatus":null}],"lastSeen":1456312407591},{"entityClass":"DefaultEntityClass","mac":["6e:c3:e4:5e:1f:65"],"ipv4":["10.0.0.4"],"ipv6":[],"vlan":["0x0"],"attachmentPoint":[{"switchDPID":"00:00:00:00:00:00:00:03","port":2,"errorStatus":null}],"lastSeen":1456312407626}] 

出于某种原因,仅将.getString(“switchDPID”)更改为.getString(“mac”)不起作用。显然,使用“mac”的结果不是一个字符串,我无法弄清楚我应该使用什么。我在这里做错了什么,我应该改变什么?这是mac地址格式的问题还是与JSON格式有关?

+0

你可以看看杰克逊API,这里提到的例子是http://www.mkyong.com/java/jackson-2-convert-java-object-to-from-json/ – Rupesh

mac的值是一个数组。所以你必须在这里使用getJSONArray("mac")。确切的语法取决于您使用的JSON库。

您可能想看看Gson,它可以将JSON转换为常规的Java对象并返回。

mac是一个JSON数组。

在该第一示例中,层次结构是如下

JSON阵列 - > JSON对象 - > switchDPID

在第二个层次是

JSON阵列 - > JSON对象 - > JSON数组(mac) - > mac内的数据。

如果您在树形结构中看到JSON,则可以看到差异。 我使用http://www.jsoneditoronline.org/

您将不得不首先获取JSON数组(mac),然后访问它的数据的第一个元素。

这里,mac是一个数组。尝试读取它作为一个数组,然后获取数组中的第一个项目作为字符串!