Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何检查每个Json对象的某个键是否具有特定值?_C#_Json_Unit Testing_Foreach - Fatal编程技术网

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

C# 如何检查每个Json对象的某个键是否具有特定值?,c#,json,unit-testing,foreach,C#,Json,Unit Testing,Foreach,我正在编写一个单元测试来检查通过参数搜索是否返回正确的数据。例如,通过搜索参数“John”搜索“employees”表中的“firstName”列时,应仅返回名为“John”、“Johnny”、“John”等的员工。对于返回的每个员工,我在JSon文件中都会得到类似的结果: { "_index": "employees", "_id": "xyz", "_score": 4.306519, "_source": { "id": 51, "districtId": 71,

我正在编写一个单元测试来检查通过参数搜索是否返回正确的数据。例如,通过搜索参数“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循环,循环遍历所有员工,看看每个员工的名字是否对应于搜索参数,如下所示:

foreach(员工中的var员工返回)
{
...
Assert.True(employee.Contains)(searchParameter,
StringComparison.CurrentCultureInogoreCase);
}

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

对于真正的单元测试,您应该能够在搜索结果序列化之前对其进行测试。这也会使你的支票更容易。如果这是不可能的,反序列化响应并进行检查对我来说似乎很好。您能解释一下您是如何通过单元测试设置正在搜索的employees表的吗?