Java如何访问JSON内部子数组

6
我有一个类似下面的JSON结构(帖子末尾), 我想从文件中读取它并提取一些信息。我想获取"times"子项并对其进行操作。我尝试使用json.simple和一些jackson库,但是我一直收到转换/类型错误。我很困惑 :/
首先,我读入文件,看起来已经正确捕获:
JSONParser parser = new JSONParser();
JSONObject data = (JSONObject) parser.parse(new FileReader("test.json"));

我尝试跟随这个链接:Java JSONObject get children,但是出现了错误 org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject

我的目的(认为是这样?)是创建一个时间的JSON,然后可以从中制作时间列表/进行各种操作。或者最好的方法是使用ObjectMapper并将其映射到匹配的对象中?

{
    "id": "ca1b57be-6c38-4976-9050-f9a95a05a38d",
    "name": "some name",
    "results": [
        {
            "name": "https://st-dev.aexp.com/smart-test/v1/test/test",
            "tests": {
                "name": "Body matches string",
                "status": "pass",
                "Response time is less than 200ms": true
            },
            "testPassFailCounts": {
                "Body matches string": {
                    "pass": 100,
                    "fail": 0
                },
                "Response time is less than 200ms": {
                    "pass": 100,
                    "fail": 0
                }
            },
            "times": [
                "48",
                "25",
                "25",
                "28",
                "24",
                "24",
                "35",
                "29",
                "41",
                "28",
                "28",
                "24",
                "31",
                "28",
                "25",
                "27",
                "23",
                "28",
                "44",
                "29",
                "25",
                "23",
                "44",
                "28",
                "22"
            ]
        }
    ]       
}

非常感谢您的帮助!
2个回答

4
这里是使用json.org库的另一种实现方式。
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class ProcessJson {
    public void test(String str) throws JSONException {
        JSONObject json = new JSONObject(str);  //initial JSONObject (See explanation section below)
        JSONArray jsonArray = json.getJSONArray("results");  //"results" JSONArray
        JSONObject item = jsonArray.getJSONObject(0);  //first JSONObject inside "results" JSONArray
        JSONArray jsonArrayTimes = item.getJSONArray("times");  //"times" JSONArray

        for (int i = 0; i < jsonArrayTimes.length(); i++) {
            System.out.println(jsonArrayTimes.getInt(i));
        }
    }
}

Maven的pom.xml中的依赖关系

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.json/json -->
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20160810</version>
    </dependency>
</dependencies>

输出结果为:
48
25
25
28
24
24
35
29
41
28
28
24
31
28
25
27
23
28
44
29
25
23
44
28
22

解释:

{ } = JSONObject
[ ] = JSONArray

“times” JSONArray嵌套在“results” JSONArray的第一个JSONObject中。

这是简化结构:

{ 
    "results": [
        {
            "times": [
                "48",
                "25", ...
            ]

        }
    ]
}

0

可能有更好的方法来完成这个任务,但使用Jackson的ObjectMapper,这里是一种解决方案:

String json = readJSON(); // <- read the JSON from somewhere as a plain String
Map<String, Object> jsonDocument = new ObjectMapper()
            .readValue(json, new TypeReference<Map<String, Object>>() {});

List<Object> resultsList = (List<Object>) jsonDocument.get("results");
Map<String, Object> resultsMap = (Map<String, Object>) resultsList.get(0);
List<Integer> times = (List<Integer>) resultsMap.get("times");

// process the times

基本上,您的示例 JSON 只适用于 Map<String, Object>,您需要逐个处理元素,检查文档并添加相应的类转换(MapList)。Jackson 将 JSON 的名称-值对(在文档中由 {} 表示)转换为 Map,将数组(由 [] 表示)转换为 List。上面的代码片段假定 results 的值始终是单个元素列表。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接