2016-07-04 122 views
0

到第三方API的調用嵌套項條件返回JSON象下面這樣:獲取JSON嵌套值與Java中

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

這表明,對於測試計劃SamplePlan id爲515,它有3項其中ID爲7857aab1-b4a3-460f-86ae-0392a128c6f5,c68a3358-dacd-4b5a-b1a8-3632b4df6476bb7318fe-f04f-42e9-985b-19aae697eba6

在完整回覆中,我想從all the entries處取得one specific entry id。我可以考慮選擇一個獨特的名稱/值對應用這種情況。

例如,如果我要拉的第一個條目ID IE 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 
} 

有AA在Java中實現這種事情的方式?考慮這種情況的一個例子會很好。

回答

0

您可以使用類似JsonPath的圖書館。 它允許您在基於xml的內容上對類似於xpath的json對象執行查詢。

你的第一個例子看起來有點像這樣(未經測試!):

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

編輯:看this answer