REST确保仅从响应正文

时间:2017-08-21 23:07:34

标签: groovy rest-assured rest-assured-jsonpath

想象一下,我调用了一个RESTful服务,它在响应主体中返回

[
  {
    "key": "Reason1 key",
    "values": {
      "en": "Reason1 English translation",
      "es": "Reason1 Spanish translation"
    }
  },
  {
    "key": "Reason2 key",
    "values": {
      "en": "Reason2 English translation",
      "es": "Reason2 Spanish translation"
    }
  }
]

我只需要获取地图键的值是' en'

所以我希望过滤后的结果就像那样

  

[" Reason1英文翻译"," Reason2英文翻译"]

     

OR

     

{" Reason1英文翻译"," Reason2英文翻译"}

我已经尝试了下一段代码,但仍未完成

response.then().body("findAll { it }.collect { it.values }", hasItems("Reason1 English translation", "Reason2 English translation"))

然后返回

  

[{en = Reason1英文翻译,es = Reason1西班牙语翻译},{en = Reason2英文翻译,es = Reason2西班牙语翻译}]

那么我怎么才能得到匹配映射关键条件的映射值' en'?

1 个答案:

答案 0 :(得分:1)

....body("findAll { it }.collect { it.values.en }"

的更改

添加en以获取en个节点,而不是整个values节点。