如何检查每个Json对象的某个键是否具有特定值?

时间:2019-10-31 09:37:14

标签: c# json unit-testing foreach

我正在编写一个单元测试,以检查按参数搜索是否返回正确的数据。例如,通过搜索参数“ John”搜索“ employees”表中的“ firstName”列,应仅返回名称为“ John”,“ Johnny”,“ john”等的员工。对于返回的每个员工,我在JSon文件中都得到了类似的内容:

{
 "_index": "employees",
 "_id": "xyz",
 "_score": 4.306519,
 "_source": {
    "id": 51,
    "districtId": 71,
    "firstName": "John",
    "lastName": "Smith",
    "divisionId": 20,
    "phone": "29437250"
}

我想编写一个foreach循环来遍历所有雇员,看看每个雇员的firstName是否对应于搜索参数,像这样:

foreach (var employee in employeesReturned)
{
    ...
    Assert.True(employee.Contains(searchParameter,
        StringComparison.CurrentCultureIgnoreCase));
}

但是如何正确编写呢?我应该以某种方式遍历整个JSon文件吗?我应该反序列化吗?最佳做法是什么?

0 个答案:

没有答案