放心 – 如何验证具有相同名称的JSON响应字段

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

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);

huangapple
  • 本文由 发表于 2020年8月27日 04:34:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63605359.html
匿名

发表评论

匿名网友

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

确定