【问题标题】:Gatling: Map fields from Json Response to a Map ObjectGatling:将 Json 响应中的字段映射到映射对象
【发布时间】:2020-11-21 06:09:11
【问题描述】:

我的回复是这样的

{
   "data":[
      {
         "id":"919274ee42fe01d40f89f51239009a2b",
         "descriptor":"Long Count_copy_1938",
         "alias":"longCount_Copy_1938",
         "type":"Numeric"
      },
      {
         "id":"435274ee42fe01d40f89f51239009a2b",
         "descriptor":"Long Count2",
         "alias":"longCount_Copy2",
         "type":"Numeric"
      },
      {
         "id":"345274ee42fe01d40f89f51239009a2b",
         "descriptor":"Short Count2",
         "alias":"Short count",
         "type":"Numeric"
      }
   ]
}

我想将 "descriptor":"id" 提取到地图中。映射后,Map 对象应如下所示

"Long Count_copy_1938" -> "919274ee42fe01d40f89f51239009a2b"
"Long Count2" -> "435274ee42fe01d40f89f51239009a2b"
"Short Count2" -> "345274ee42fe01d40f89f51239009a2b"

这是我实现它的方法,如果有更好的方法,请告诉我。谢谢!

exec(http("Get Field ids")
  .get(s"${wqlDataSources}/")
  .check(status.is(200),
  jsonPath("$.data[*].descriptor").findAll.saveAs("descriptors"),
  jsonPath("$.data[*].id").findAll.saveAs("ids")))
.exec(session => {
  val descriptors = session("descriptors").as[Vector[String]]
  val ids = session("ids").as[Vector[String]]
  val fieldIdMap = (descriptors zip ids).toMap
  session.set("fieldIdResultMap", fieldIdMap)
  session.remove("descriptors").remove("ids")
})

【问题讨论】:

    标签: gatling jsonpath


    【解决方案1】:

    大多数 JSONPath 实现支持使用 [,] 联合运算符一次性提取多个值,例如$..['id','descriptor'] 匹配您的两个属性。
    (但是,联合的可用性和结果既不通用也不一致,如果您通过切换到 Goessner 或 Jayway 检查上述路径 online here,您会注意到结果不一样,测试站点上的 Gatling 选项卡甚至会抛出错误;如果站点使用最新版本,我不知道它是否可以工作。)

    我找不到任何确认 Gatling 支持工会的文档,但我在 Gatling 的 Github 存储库中找到了 this test 有一个工会:JsonPath.query("$.menu['file','year']", json) ... 所以,一般来说,工会应该可以工作。

    经过反复试验,我发现这条路径可以使用 Gatling(即使是旧版本):

    $.data[*]['id','descriptor']
    

    返回:

    [
       "919274ee42fe01d40f89f51239009a2b",
       "Long Count_copy_1938",
       "435274ee42fe01d40f89f51239009a2b",
       "Long Count2",
       "345274ee42fe01d40f89f51239009a2b",
       "Short Count2"
    ]
    

    因此,您应该能够使用此路径一次性映射键/值对!

    【讨论】:

    • 非常感谢!这是我所做的,{ exec(http("Get Field ids for the data source") .get(s"${wqlDataSources}/" + "${datasourceId}/fields?offset=${offset}&limit=100 ") .check(status.is(200), jsonPath("$.data[].descriptor").findAll.saveAs("descriptors"), jsonPath("$.data[]. id").findAll.saveAs("ids"))) .exec(session => { val descriptors = session("descriptors").as[Vector[String]] val ids = session("ids").as[ Vector[String]] val currentFieldIdMap = (descriptors zip ids).toMap })
    • @TaraSudarshan 太棒了。我很高兴这很有帮助。赞成。
    【解决方案2】:

    这里还有 1 个有效的解决方案,

    exec(http("Get Field ids for the data source")
      .get(s"${wqlDataSources}/" + "${datasourceId}/fields)
      .check(status.is(200),
      jsonPath("$.data[*].descriptor").findAll.saveAs("descriptors"),
      jsonPath("$.data[*].id").findAll.saveAs("ids")))
    .exec(session => {
      val descriptors = session("descriptors").as[Vector[String]]
      val ids = session("ids").as[Vector[String]]
      val currentFieldIdMap = (descriptors zip ids).toMap
    })
    

    【讨论】:

      猜你喜欢
      • 2023-03-10
      • 2019-05-27
      • 2016-06-24
      • 2019-09-24
      • 2012-04-23
      • 2020-08-08
      • 2019-07-22
      • 1970-01-01
      • 2021-07-06
      相关资源
      最近更新 更多