简体   繁体   English

加特林将Json数组转换为Map

[英]Gatling convert Json array to Map

I have json such as ["123","123a","12c3","1f23","e123","r123"] as response from rest server. 我有JSON,例如[“ 123”,“ 123a”,“ 12c3”,“ 1f23”,“ e123”,“ r123”]作为来自其他服务器的响应。

I want to parse this json as Collection and iterate over it and make exec request over each element in it such as : 我想将此json解析为Collection并对其进行迭代,并对其中的每个元素发出exec请求,例如:

SERVER + "/get?param=${el}" where el will be 123,123a,12c3,1f23,e123 and r123 服务器+“ / get?param = $ {el}”,其中el将是123,123a,12c3,1f23,e123和r123

My question is how can I do it. 我的问题是我该怎么做。

You can do something like this: 您可以执行以下操作:

import org.json4s._
import org.json4s.jackson.JsonMethods._
object JSonToMap {
  def main(args: Array[String]) {
    implicit val fmt = org.json4s.DefaultFormats
    val json = parse("""{ "response" : ["123","123a","12c3","1f23","e123","r123"] }""")
    val jsonResp = json.extract[JsonResp]
    println(jsonResp)
    jsonResp.response.foreach { param => 
      println(s"SERVER /get?param=${param}")
    }

  }
  case class JsonResp(response: Seq[String], somethingElse: Option[String])
}

Now you have a case class where the "response" member is a list of your strings. 现在,您有了一个案例类,其中“响应”成员是您的字符串列表。 You can then manipulate this list however you need to create the requests to SERVER. 然后,您可以操作此列表,但是需要创建对SERVER的请求。

You should try something like this: 您应该尝试这样的事情:

exec(
  http("my request")
    .get("/myrequest")
    .check(status.is(200))
    .check(jsonPath("$").ofType[Seq[String]].saveAs("params"))
).foreach("${params}", "param") {
  exec(http("request with parameter ${param}")
    .get("/get")
    .queryParam("param", "$param")
  )
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM