use*_*958 11 json scala gatling
我试图解析服务器返回给gatling的json响应.
我对服务器的回复是:
SessionAttribute(
Session(
GetServices,
3491823964710285818-0,
Map(
gatling.http.cache.etagStore -> Map(https://api.xyz.com/services -> ),
gatling.http.cache.lastModifiedStore -> Map(https://api.xyz.com/services -> ),
myresponse -> {
"created":"2014-12-16T22:06:59.149+0000",
"id":"x8utwb2unq8uey23vpj64t65",
"name":"myservice",
"updated":"2014-12-16T22:06:59.149+0000",
"version":null
}),
1418767654142,622,
OK,List(),<function1>),id)
Run Code Online (Sandbox Code Playgroud)
我在我的脚本中这样做:
val scn = scenario("GetServices")
.exec(http("Get all Services")
.post("/services")
.body(StringBody("""{ "name": "myservice" }""")).asJSON
.headers(sentHeaders)
.check(jsonPath("$")
.saveAs("myresponse"))
).exec(session => {
println(session.get("id"))
session
})
Run Code Online (Sandbox Code Playgroud)
它仍在打印整个响应.我怎样才能检索到的id是"x8utwb2unq8uey23vpj64t65"
什么?
mil*_*use 24
这可能是最容易使用的多一点点jsonPath拔出id
你的需要,存储的是在自己的供以后使用的变量.请记住,jsonPath
它仍然是一个CheckBuilder,因此您不能直接访问结果 - 它可能无法匹配.
将它转换为一个Option[String]
似乎是合理的事情.
所以你的最后几行将成为:
...
.check(
jsonPath("$.id").saveAs("myresponseId")
)
)
).exec(session => {
val maybeId = session.get("myresponseId").asOption[String]
println(maybeId.getOrElse("COULD NOT FIND ID"))
session
})
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
21835 次 |
最近记录: |