Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java如何访问JSON内部子数组_Java_Json_Jackson - Fatal编程技术网

Java如何访问JSON内部子数组

Java如何访问JSON内部子数组,java,json,jackson,Java,Json,Jackson,我有一个JSON,它的结构与下面的类似(文章末尾)。我试图从一个文件中读取它,然后提取一些信息。我想找个《泰晤士报》的孩子做点什么。我尝试过使用json.simple和一些jackson的东西,但我总是会遇到铸造/类型错误。我被卡住了:/ 首先,我在文件中读到,它似乎被正确捕获: JSONParser parser = new JSONParser(); JSONObject data = (JSONObject) parser.parse(new FileReader("test.json")

我有一个JSON,它的结构与下面的类似(文章末尾)。我试图从一个文件中读取它,然后提取一些信息。我想找个《泰晤士报》的孩子做点什么。我尝试过使用json.simple和一些jackson的东西,但我总是会遇到铸造/类型错误。我被卡住了:/

首先,我在文件中读到,它似乎被正确捕获:

JSONParser parser = new JSONParser();
JSONObject data = (JSONObject) parser.parse(new FileReader("test.json"));
然后我试着这样做: 但是get errors
org.json.simple.JSONArray不能强制转换为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"
            ]
        }
    ]       
}

谢谢你的帮助

可能有更好的方法,但是使用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

String json=readJSON();// 下面是另一个使用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));
        }
    }
}
说明:

{ } = JSONObject
[ ] = JSONArray
“times”JSONArray嵌套在“results”JSONArray的第一个JSONObject中

以下是简化结构:

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

        }
    ]
}
{ } = JSONObject
[ ] = JSONArray
{ 
    "results": [
        {
            "times": [
                "48",
                "25", ...
            ]

        }
    ]
}