Gatling - 循环遍历 JSON 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25289334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:29:17  来源:igfitidea点击:

Gatling - Looping through JSON array

jsonscalajsonpathgatling

提问by Neil

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.)

我有一段代码需要遍历从 REST 服务的响应中获取的 JSON 数组。(此处提供完整要点。)

.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
    }
  ]
}

And playerIdshows up in the session as

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)}

Expected : 3 requests with body as

预期:3 个请求,正文为

 {"playerId":1}
 {"playerId":2}
 {"playerId":3}

I can loop over the resulting array successfully if I save just the playerIds:

如果我只保存 playerIds,我可以成功地遍历结果数组:

.check(jsonPath("$.data[*].playerId").findAll.saveAs("pList")))

回答by Michelle

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:

我设法将您要查找的请求发送出去(尽管仍然收到 404,但这可能是服务器端的,或者您的 Gist 发送的请求可能丢失了某些内容)。诀窍是完全放弃 JsonPath:

.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)
  })

Here, the jsonPathcheck 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 Intor Longif all your values are numbers. If you want more info on what went wrong with JsonPath, read on.

在这里,jsonPathcheck 可以自动将你的 JSON 对象存储为地图,然后你就可以通过 key 访问玩家 ID。值类型不必是Any,您可以使用Int或者Long如果您的所有值都是数字。如果您想了解更多有关问题的信息,请继续JsonPath阅读。



Your first problem is that JsonPath.query()doesn't just return the value you're looking for. From the JsonPath readme:

您的第一个问题是,JsonPath.query()它不仅仅返回您要查找的值。来自JsonPath 自述文件

JsonPath.query("$.a", jsonSample) gives you Right(non-empty iterator). This will allow you to iterate over all possible solutions to the query.

JsonPath.query("$.a", jsonSample) 为您提供 Right(非空迭代器)。这将允许您迭代查询的所有可能解决方案。

Now, when it says Right(non-empty iterator), I assumed that meant the iterator was not empty. However, if you try this:

现在,当它说 时Right(non-empty iterator),我认为这意味着迭代器不为空。但是,如果您尝试这样做:

val playerId = JsonPath.query("$.playerId", session("player").as[String]).right.get
println(playerId)

...it prints "empty iterator". I'm not sure whether it's a problem with JsonPath, the jsonPathcheck, or usage somewhere in between, but there's not quite enough documentation for me to want to dig into it.

...它打印“空迭代器”。我不确定它是否存在问题JsonPathjsonPath检查或介于两者之间的用法,但没有足够的文档让我想要深入研究它。