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

How to parse JSONObjects in a JSONArray in Java?

我正在尝试从 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") 是行不通的。显然使用 "mac" 的结果不是字符串,我不知道应该使用什么。我在这里做错了什么,我应该改变什么?这是 mac-地址格式的问题还是与 JSON 格式有关?

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是一个数组。尝试将其作为数组读取,然后将数组中的第一项作为字符串获取!