[英]How to assert json response content using groovy?
in the response of a request i have this content: 在请求的响应中,我具有以下内容:
"comp":[
{
"type":"header",
"version":1,
"settings":
{"logo":"mylogo",
"logoPosition":"left",
"inverseLogosPosition":false,
"headerTitle":"My Report",
"headerTitlePosition":"left",
"pageBreak":false
}
},
I want to assert the content of settings. 我想断言设置的内容。 i try this for example to assert the logoPosition = "left"
我尝试例如断言logoPosition =“ left”
assert json.components.settings[0].logoPosition[0] == "left" 断言json.components.settings [0] .logoPosition [0] ==“左”
it's not working 它不起作用
This part is working well: 这部分工作正常:
assert json.comp.type[0] == "header"
assert json.comp.version[0] == 1
Any help please, thank you 任何帮助,谢谢
The json provided is invalid. 提供的json无效。 You can use both paths :
您可以使用两种路径 :
assert slurped.comp.settings.logoPosition[0] == "left"
assert slurped.comp[0].settings.logoPosition == "left"
Full example: 完整示例:
import groovy.json.JsonSlurper
def json = '''{
"comp":[
{
"type":"header",
"version":1,
"settings": {
"logo":"mylogo",
"logoPosition":"left",
"inverseLogosPosition":false,
"headerTitle":"My Report",
"headerTitlePosition":"left",
"pageBreak":false
}
}
]}'''
def slurped = new JsonSlurper().parseText(json)
assert slurped.comp.settings.logoPosition[0] == "left"
assert slurped.comp[0].settings.logoPosition == "left"
It will just be logoPosition
, not logoPosition[0]
只会是
logoPosition
,而不是logoPosition[0]
Why not have some expected json as a string, convert it to a map with JsonSlurper, then compare these? 为什么不将一些期望的json作为字符串,使用JsonSlurper将其转换为映射,然后进行比较?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.