英文:
Rest assured - How to validate JSON response field with the same name
问题
我有样本JSON响应
"userinfo": [
{
"userName": "name1",
"registered": false
},
{
"userName": "name2",
"registered": true
}
]
当 userName = name1
时,我如何验证 registered = false
?
当 userName = name2
时,我如何验证 registered = true
?
尝试了类似这样的代码,但它不起作用。
.body("$.findAll{it.userinfo.userName = name1}.registered", equalTo(false));
我有什么遗漏或做错了什么吗?
英文:
I have the sample JSON response
"userinfo": [
{
"userName": "name1",
"registered": false
},
{
"userName": "name2",
"registered": true
}
]
How can I verify when userName = name1
then registered = false
When userName = name2
then registered = true
Tried something like this but it does not work.
.body("$.findAll{it.userinfo.userName = name1"}.registered", equalTo(false));
Did I miss something or do something wrong?
答案1
得分: 1
这是可以帮助您的内容:
String json = "{
\"userinfo\": [{
\"userName\": \"name1\",
\"registered\": false
},
{
\"userName\": \"name2\",
\"registered\": true
}]
}";
JsonPath js = new JsonPath(json);
String emp1Name = js.get("userinfo.find {it.userName == 'name2'}.registered").toString();
System.out.println(emp1Name);
英文:
Here's something that can help you out,
String json = "{\r\n" + " \"userinfo\": [{\r\n" + " \"userName\": \"name1\",\r\n"
+ " \"registered\": false\r\n" + " },\r\n" + " {\r\n"
+ " \"userName\": \"name2\",\r\n" + " \"registered\": true\r\n" + " }\r\n"
+ " ]\r\n" + "}";
JsonPath js = new JsonPath(json);
String emp1Name = js.get("userinfo.find {it.userName == 'name2'}.registered").toString();
System.out.println(emp1Name);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论