将Map <string,object =“”>转换为List <jsonobjects>

时间:2017-10-13 11:07:12

标签: java json spring jackson

我有一个函数可以调用竹子休息ap并恢复构建结果。该方法不仅仅是检索失败的测试并将它们放入HashMap,而不是将其发送到Web应用程序并进行处理。

public Map<String, Object> retrieveLatestBuildResultDetails(String planKey) {

    // BAMBOO API CALL 

    if (response != null) {
        Map<String, Object> result = new HashMap<>();

        List resultDetails = (List) ((Map) ((Map) response.getBody() 
              .get("testResults")).get("failedTests")).get("testResult");

        result.put("details", resultDetails);
        return result;
    }
    return null;
}

收到的Json看起来像这样:

"successfulTests": {
  "size": 3,
  "start-index": 0,
  "max-result": 3
},
"failedTests": {
  "size": 13,
  "expand": "testResult",
  "testResult": [{
      "expand": "errors",
      "className": "SomeTest",
      "methodName": "SomeMethodNAme",
      "status": "failed",
      "duration": 2,
      "durationInSeconds": 0,
      "errors": {
        "size": 1,
        "error": [{
          "message": "something"
        }],
        "start-index": 0,
        "max-result": 1
      }
    },
    {
      "expand": "errors",
      "className": "SomeTest",
      "methodName": "SomeMethodNAme",
      "status": "failed",
      "duration": 2,
      "durationInSeconds": 0,
      "errors": {
        "size": 1,
        "error": [{
          "message": "something"
        }],
        "start-index": 0,
        "max-result": 1
      }
    }
  ],
  "start-index": 0,
  "max-result": 13
},

我现在想要使用HashMap并在不改变原始方法的情况下进一步处理它,因为服务依赖于该格式。 我现在的问题是如何从失败的测试中访问每个对象。 目前,我的新方法如下所示:

List<Object> failedTestResults = (List) buildResultDetails.get("details");

for (Object failedTest : failedTestResults) {

    JSONObject failedTestJSON = new JSONObject(failedTest);
    String className = failedTestJSON.getString("className");
    String methodName = failedTestJSON.getString("methodName");

    JSONObject errors = failedTestJSON.getJSONObject("errors");
    JSONArray message = errors.getJSONArray("error");


    String detailText = "";
    for(int i=0; i<message.length(); i++){
        JSONObject errorMessage = message.getJSONObject(i);
        // Splitting string at the first linebreak to only 
        // get the first line of the Exception
        detailText += errorMessage.getString("message").split("\\n", 2)[0];
    }
}

我在俯瞰什么?

0 个答案:

没有答案