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

时间:2016-02-29 10:41:11

标签: java arrays json jsonobject

我正在尝试从Floodlight(SDN控制器)REST API读取数据并将其输入到其他软件的REST API中。我有这个从Floodlight的REST API中读取的内容:

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输出,我可以用它来做更简单的事情:

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”)flat out无法正常工作。显然,使用“mac”的结果不是字符串,我无法弄清楚我应该使用什么。我在这里做错了什么,我应该改变什么?这是mac-address格式的问题还是与JSON格式有关?

3 个答案:

答案 0 :(得分:0)

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

您可能希望查看Gson,它可以将JSON转换为常规Java对象,然后再返回。

答案 1 :(得分:0)

promt是一个JSON数组。

在第一个例子中,层次结构如下

JSON数组 - &gt; JSON对象 - &gt; switchDPID

在第二个层次结构中

JSON数组 - &gt; JSON对象 - &gt; JSON数组(mac) - &gt; mac数组中的数据。

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

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

答案 2 :(得分:0)

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