简体   繁体   English

放心检查名称是否存在于json响应中

[英]rest assured check the name is exist in the json response

Am new to rest assured.Using rest assured am trying to verify data detail is found or not.Here two data details present.Some times it will be 2 or 3 or 5 Am getting response as follows and using java 我是放心的新人。使用放心的我试图验证是否找到了数据详细信息。这里有两个数据详细信息。有时是2或3或5 am会得到如下响应并使用java

{
  "queryPath": "/api/",
  "nId": "f084f5ad24fcfaa9e9faea0",
  "statusCode": 707
  "statusMessage": "Success",
  "results": {
    "data": [
      {
        "id": "10248522500798",
        "capabilities": [
          "record",
          "HDt"
        ],
        "name": "errt2"
      },
      {
        "id": "418143778",
        "capabilities": [
          "1record",
          "HDy"
        ],
        "name": "Livin"
      }
    ]
  }
}

code using 使用代码

JsonPath jsonResponse = new JsonPath(response.asString());
ArrayList<String> list = new ArrayList<String>();
list = jsonResponse.get("results.data"); // 

if (list.size() < 1 ) {
    SpiceCheck.fail("data not found! " + list.size());
}

Rather than this i wwant to check the data name is null or not also.How can i do that rest assured. 而不是我想检查数据名称是否为null。我该如何保证。

Just so you know you are missing a comma after 707. 如此一来,您就知道707之后缺少逗号。

To verify that none of the names is null I would parse out the names as a list, then iterate over the names one by one and check that they aren't null. 为了验证所有名称都不为空,我将名称解析为列表,然后逐个迭代名称,并检查它们是否不为空。

List<String> names = from(response.asString()).getList("results.data.name");
for(String name : names){
  if(name == null){
     // error handling code here
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM