我是加特林的新手,在這方面有點掙扎。
我有一個來自 HTML 的 JSON 物件:
<div id="DATA--DECL-DATA">{"isCompany":false,"accommodations":[{"id":"00000000031000000067","isChecked":false,"name":"5 JULI 2017","addressLine1":"STRAAT 10 ","addressLine2":"1000 New York","nightsDeclared":0,"schoolNightsDeclared":0,"schoolNightsAttached":0,"taxableNights":0.0,"totalPayment":0.0,"isInProgress":false,"isLate":false,"isPayed":"false","deadline":"2021-12-31","initialAmount":0.0,"remainingAmount":0.0},{"id":"00000000031000006362","isChecked":false,"name":"BELLEVIE","addressLine1":"STRAAT 10 ","addressLine2":"1000 New York","isInProgress":false},{"id":"00000000031000006380","isChecked":false,"name":"BELLEVIE","addressLine1":"STRAAT 10 ","addressLine2":"1000 New York","isInProgress":true},{"id":"00000000031000006390","isChecked":false,"name":"BELLEVIE","addressLine1":"STRAAT 10 ","addressLine2":"1000 New York","isInProgress":true}]}</div>
如果美化,呈現這個:
{
"isCompany": false,
"accommodations": [
{
"id": "00000000031000000067",
"isChecked": false,
"name": "5 JULI 2017",
"addressLine1": "STRAAT 10 ",
"addressLine2": "1000 New York",
"isInProgress": false
},
{
"id": "00000000031000006362",
"isChecked": false,
"name": "BELLEVIE",
"addressLine1": "STRAAT 10 ",
"addressLine2": "1000 New York"
"isInProgress": false
},
{
"id": "00000000031000006380",
"isChecked": false,
"name": "BELLEVIE",
"addressLine1": "STRAAT 10 ",
"addressLine2": "1000 New York",
"isInProgress": true
},
{
"id": "00000000031000006390",
"isChecked": false,
"name": "BELLEVIE",
"addressLine1": "STRAAT 10 ",
"addressLine2": "1000 New York",
"isInProgress": true
}
]
}
為了從那個 div 中獲取這個 JSON 陣列并將它保存到 Gatling 的會話變數中,我寫了這個“檢查”:
.check(css("div#DATA--DECL-DATA").saveAs("myJsonObj"))
然后在執行腳本后在控制臺中列印結果,我寫了這個:
.exec { session => println("json = " session("myJsonObj").as[String]); session }.exitHereIfFailed
這將在控制臺中列印出您在上面看到的完整的美化 JSON 陣列。
現在在 JSON Array 中,我們可以看到當“isInProgress”為 false時有幾個 id 。
那么我的問題是,當“isInProgress”為假時,如何獲取住宿的第一個 id ?
所以:如果“IsInProgress”為假 => 獲取該陣列中的第一個住宿 ID。
uj5u.com熱心網友回復:
以下代碼使用 Javascript,但應該很容易轉換為 Scala。
var json = {
"isCompany": false,
"accommodations": [
{
"id": "00000000031000000067",
"isChecked": false,
"name": "5 JULI 2017",
"addressLine1": "STRAAT 10 ",
"addressLine2": "1000 New York",
"isInProgress": false
},
{
"id": "00000000031000006362",
"isChecked": false,
"name": "BELLEVIE",
"addressLine1": "STRAAT 10 ",
"addressLine2": "1000 New York",
"isInProgress": false
},
{
"id": "00000000031000006380",
"isChecked": false,
"name": "BELLEVIE",
"addressLine1": "STRAAT 10 ",
"addressLine2": "1000 New York",
"isInProgress": true
},
{
"id": "00000000031000006390",
"isChecked": false,
"name": "BELLEVIE",
"addressLine1": "STRAAT 10 ",
"addressLine2": "1000 New York",
"isInProgress": true
}
]
}
var FilteredJson = json.accommodations.filter((value) => {if (value.isInProgress === false) {return value}});
//just take the first value of FilteredJson
console.log(FilteredJson[0]);
uj5u.com熱心網友回復:
根據我之前的回答,您需要check一個帶有條件的 json-path:
.check(jsonPath("$.accommodations[?(@.isInProgress==false)].id").find.saveAs("id"))
解釋:
@.isInProgress==false- 它是 array 中每個物件的條件accommodations。可以在此處找到有關 json-path 的更多詳細資訊
Gatling 的方法find回傳第一次出現。也存在findAll回傳所有值的方法......
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/389450.html
