【问题标题】:Java How to access JSON inner child arrayJava如何访问JSON内部子数组
【发布时间】:2017-04-21 21:03:03
【问题描述】:

我有一个与下面的结构相似的 JSON(帖子结束)。我正在尝试从文件中读取它,然后提取一些信息。我想得到“时代”孩子并用它做点什么。我尝试过使用 json.simple 和一些杰克逊的东西,但我不断收到转换/类型错误。我很困惑:/

首先我读入了文件,它似乎被正确捕获:

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"
            ]
        }
    ]       
}

非常感谢您的帮助!

【问题讨论】:

    标签: java json jackson


    【解决方案1】:

    这是另一个使用来自 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", ...
                ]
    
            }
        ]
    }
    

    【讨论】:

      【解决方案2】:

      可能有更好的方法,但使用杰克逊的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&lt;String, Object&gt;,然后您需要逐个元素处理它,检查文档并添加相应的类转换(MapList)。 Jackson 将 JSON 的名称-值对(在文档中用{}s 表示)转换为Maps,将数组(用[]s 表示)转换为Lists。上面的代码 sn-p 假设results 的值始终是一个单元素列表。

      【讨论】:

        猜你喜欢
        • 2017-10-18
        • 2019-12-08
        • 1970-01-01
        • 2016-12-02
        • 1970-01-01
        • 1970-01-01
        • 2016-12-31
        • 1970-01-01
        • 2017-02-05
        相关资源
        最近更新 更多