如何在Java中访问JSON数组?

时间:2015-10-20 10:21:23

标签: java json

我需要从我的JSON文件中获取elementId,如下所示:

{
    "intents": [{
        "type": "PathIntent",
        "id": "0x0",
        "appId": "org.onosproject.cli",
        "resources": ["DefaultLink{src=ConnectPoint{elementId=00:00:00:00:00:01/-1, portNumber=0}, dst=ConnectPoint{elementId=of:0000000000000003, portNumber=1}, type=EDGE, state=ACTIVE, durable=false}", "DefaultLink{src=ConnectPoint{elementId=of:0000000000000003, portNumber=3}, dst=ConnectPoint{elementId=of:0000000000000002, portNumber=1}, type=DIRECT, state=ACTIVE, durable=false}", "DefaultLink{src=ConnectPoint{elementId=of:0000000000000002, portNumber=2}, dst=ConnectPoint{elementId=of:0000000000000004, portNumber=3}, type=DIRECT, state=ACTIVE, durable=false}", "DefaultLink{src=ConnectPoint{elementId=of:0000000000000004, portNumber=2}, dst=ConnectPoint{elementId=00:00:00:00:00:04/-1, portNumber=0}, type=EDGE, state=ACTIVE, durable=false}"],
        "state": "INSTALLED"
    }]
}

1 个答案:

答案 0 :(得分:0)

获取JSON-Array非常简单:

JSONObject obj = new JSONObject("...");
JSONArray arr = obj.getJSONArray("resources");

您将获得此元素的数组

"DefaultLink{src=ConnectPoint{elementId=00:00:00:00:00:01/-1, portNumber=0}, dst=ConnectPoint{elementId=of:0000000000000003, portNumber=1}, type=EDGE, state=ACTIVE, durable=false}" 

不幸的是你需要解析String我猜。