使用Java

时间:2016-07-04 23:40:31

标签: java json

对第三方API的调用会返回如下所示的JSON:

{
  "id": 515,
  "name": "SamplePlan",
  "is_completed": false,
  "project_id": 9,
  "url": "https://domain.ad.com/test/index.php?/plans/view/515",
  "entries": [
    {
      "id": "7857aab1-b4a3-460f-86ae-0392a128c6f5",
      "suite_id": 108,
      "name": "ANDROID 6 BRANCH 21:10",
      "runs": [
        {
          "id": 1184,
          "suite_id": 108,
          "name": "ANDROID 6 BRANCH 21:10",
          "entry_id": "7857aab1-b4a3-460f-86ae-0392a128c6f5",
        }
      ]
    },
    {
      "id": "c68a3358-dacd-4b5a-b1a8-3632b4df6476",
      "suite_id": 108,
      "name": "ANDROID 7 BRANCH 23:10",
      "runs": [
        {
          "id": 1393,
          "suite_id": 108,
          "name": "ANDROID 7 BRANCH 23:10",
          "entry_id": "c68a3358-dacd-4b5a-b1a8-3632b4df6476",
        }
      ]
    },
    {
      "id": "bb7318fe-f04f-42e9-985b-19aae697eba6",
      "suite_id": 108,
      "name": "ANDROID 8 BRANCH 13:10",
      "runs": [
        {
          "id": 1506,
          "suite_id": 108,
          "name": "ANDROID 8 BRANCH 13:10",
          "entry_id": "bb7318fe-f04f-42e9-985b-19aae697eba6",
        }
      ]
    }
  ]
}

这表明,对于ID为SamplePlan的测试计划515,它有3个条目包含ID 7857aab1-b4a3-460f-86ae-0392a128c6f5c68a3358-dacd-4b5a-b1a8-3632b4df6476bb7318fe-f04f-42e9-985b-19aae697eba6

在完整回复中,我想从one specific entry id获取all the entries。我可以考虑选择一个唯一的名称/值对来应用这种条件。

例如,如果我想提取第一个条目ID,即7857aab1-b4a3-460f-86ae-0392a128c6f5,那么我想我会这样做:

if (inside "entries" and if name is (ANDROID 6 BRANCH 21:10) then ) {
      pull the entry id of this case;
}

另一种方法可以是:

if (inside "entries" and further inside "runs" and if (id ==1184) then) {
      pull the entry id of this case which is again 7857aab1-b4a3-460f-86ae-0392a128c6f5
}

有没有办法在Java中实现这样的东西?考虑这种情况的一个例子很棒。

1 个答案:

答案 0 :(得分:0)

您可以使用JsonPath之类的库。 它允许您对json对象执行查询,类似于基于xml的内容上的xpath。

你的第一个例子看起来有点像这样(未经测试!):

$.entries[?(@.name == "ANDROID 6 BRANCH 21:10")].id

编辑:查看this answer