加特林-遍历JSON数组 [英] Gatling - Looping through JSON array
问题描述
我有一段代码,需要遍历从REST服务的响应获得的JSON数组. (此处提供了完整的摘要.)
I have a block of code which needs to loop through a JSON array which is obtained from response of a REST service. (Full gist available here.)
.exec(http("Request_1")
.post("/endPoint")
.headers(headers_1)
.body(StringBody("""REQUEST_BODY""")).asJSON
.check(jsonPath("$.result").is("SUCCESS"))
.check(jsonPath("$.data[*]").findAll.saveAs("pList")))
.exec(session => {
println(session)
session
})
.foreach("${pList}", "player"){
exec(session => {
val playerId = JsonPath.query("$.playerId", "${player}")
session.set("playerId", playerId)
})
.exec(http("Request_1")
.post("/endPoint")
.headers(headers_1)
.body(StringBody("""{"playerId":"${playerId}"}""")).asJSON
.check(jsonPath("$.result").is("SUCCESS")))
}
第一个请求的响应格式为
The response format of the first request was
{
"result": "SUCCESS",
"data": [
{
"playerId": 2
},
{
"playerId": 3
},
{
"playerId": 4
}
]
}
playerId
在会话中显示为
pList -> Vector({playerId=2, score=200}, {playerId=3, score=200}
我在第二个请求中看到尸体是
I am seeing in the second request the body is
{"playerId":"Right(empty iterator)}
预期:3个请求,正文为
Expected : 3 requests with body as
{"playerId":1}
{"playerId":2}
{"playerId":3}
如果仅保存playerIds,我就可以成功遍历结果数组:
I can loop over the resulting array successfully if I save just the playerIds:
.check(jsonPath("$.data[*].playerId").findAll.saveAs("pList")))
推荐答案
我设法将您要查找的请求发送出去(尽管仍然收到404,但这可能是服务器端的,或者您的要旨是发送可能丢失了一些内容).诀窍是完全放弃JsonPath:
I managed to get the requests you're looking for sent out (although still getting a 404, but that might be server-side or the request your gist is sending might be missing something). The trick was to give up on JsonPath entirely:
.exec(http("Request_10")
.get("gatling1")
.headers(headers_10)
.check(jsonPath("$.result").is("SUCCESS"),
jsonPath("$.data[*]").ofType[Map[String,Any]].findAll.saveAs("pList")))
.foreach("${pList}", "player") {
exec(session => {
val playerMap = session("player").as[Map[String,Any]]
val playerId = playerMap("playerId")
session.set("playerId", playerId)
})
在这里,jsonPath
检查可以自动将您的JSON对象存储为地图,然后您可以通过密钥访问播放器ID.值类型不必为Any
,如果所有值都是数字,则可以使用Int
或Long
.如果您想了解有关JsonPath
出了什么问题的更多信息,请继续阅读.
Here, the jsonPath
check can automatically store your JSON object as a map, and then you can access the player ID by key. The value type doesn't have to be Any
, you could use Int
or Long
if all your values are numbers. If you want more info on what went wrong with JsonPath
, read on.
您的第一个问题是JsonPath.query()
不仅会返回您要查找的值.从 JsonPath自述文件:
Your first problem is that JsonPath.query()
doesn't just return the value you're looking for. From the JsonPath readme:
JsonPath.query("$.a",jsonSample)为您提供Right(非空迭代器).这将使您可以迭代查询的所有可能解决方案.
JsonPath.query("$.a", jsonSample) gives you Right(non-empty iterator). This will allow you to iterate over all possible solutions to the query.
现在,当它说Right(non-empty iterator)
时,我认为这意味着迭代器不为空.但是,如果您尝试这样做:
Now, when it says Right(non-empty iterator)
, I assumed that meant the iterator was not empty. However, if you try this:
val playerId = JsonPath.query("$.playerId", session("player").as[String]).right.get
println(playerId)
...它显示空迭代器".我不确定JsonPath
,jsonPath
检查还是两者之间的用法是否有问题,但是没有足够的文档供我研究.
...it prints "empty iterator". I'm not sure whether it's a problem with JsonPath
, the jsonPath
check, or usage somewhere in between, but there's not quite enough documentation for me to want to dig into it.
这篇关于加特林-遍历JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!