Rest-assured. 从另一个值获取值

huangapple go评论93阅读模式
英文:

Rest-assured. get value from another value

问题

"results": [
    {
      "id": "1",
      "name": "YYY",
      "shortName": "Y"
    },
    {
      "id": "2",
      "name": "XXX",
      "shortName": "X"
    },
    {
      "id": "3",
      "name": "ZZZ",
      "shortName": "Z"
    }
  ]

我想在发送名称值时获取ID值。例如,如果名称为ZZZ,则在这种情况下返回ID值3,使用Rest Assured。

英文:

I have a json response something like this:

"results": [
    {
      "id": "1",
      "name": "YYY",
      "shortName": "Y"
    },
    {
      "id": "2",
      "name": "XXX",
      "shortName": "X"
    },
    {
      "id": "3",
      "name": "ZZZ",
      "shortName": "Z"
    }
  ]

I want to get id value when I send name value. For example if name = ZZZ return me id value in this case 3 using rest assured

答案1

得分: 2

Json路径 json-path-2.9.0rest-assured

import static com.jayway.restassured.path.json.JsonPath.from;

下面是JsonPath查询:

from(response).getList("results.findAll { it.name=='ZZZ' }.id").toString() //返回 3

from(response).getList("results.findAll { it.name=='XXX' }.id").toString() //返回 2
英文:

Json path json-path-2.9.0 with rest-assured

import static com.jayway.restassured.path.json.JsonPath.from;

below JsonPath query

from(response).getList("results.findAll { it.name=='ZZZ' }.id").toString() //returns 3

from(response).getList("results.findAll { it.name=='XXX' }.id").toString() //returns 2

huangapple
  • 本文由 发表于 2020年10月22日 03:15:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64470260.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定