简体   繁体   English

如何使用C#在JSON响应中遍历具有相同名称的多个子节点并声明正确的内容

[英]How to iterate through multiple child nodes with same name in a json response using C# and assert right content

I am getting below response from a webservice . 我正在从Web服务获得以下响应。 I want to write a test for this using specflow , and C# Nunit framework 我想使用specflow和C#Nunit框架为此编写测试

{
   "status": "Healthy",
   "results":    [
            {
              "name": "Microservice one",
              "status": "Healthy",
              "description": "Url check MSOneURI success : status(OK)"
            },
            {
              "name": "Microservice two",
              "status": "Healthy",
              "description": "Url check MSTwoURI success : status(OK)"
            },
            {
              "name": "Microservice three",
              "status": "Healthy",
              "description": "Url check MSThreeURI success : status(OK)"
              },
            {
              "name": "Microservice four",
              "status": "Healthy",
              "description": "Url check MSFourURI success : status(OK)"
              },
            {
              "name": "Microservice five",
              "status": "Healthy",
              "description": "Url check MSFiveURI success : status(OK)"
              }
                ]

} }

This is how my feature file looks 这是我的功能文件的外观

@uService
Feature: Micro Service - health check
In order to perform health check 
As a service
I want to ensure downstream Micro Services are healthy 

Scenario Outline: [uService] Status of downstream microservices are healthy
Given health check micro service
When health check is performed      
Then <nameoftheservice> service returns correct description and status


Examples: 
| downstreamservice   | 
| Microservice one    | 
| Microservice two    | 
| Microservice three  | 
| Microservice four   | 
| Microservice five   | 

I need help with writing the binding method for the Then step 我需要帮助为Then步骤编写绑定方法

You could deserialize the JSON with the Newtonsoft.JSON package then iterate over the list of services with LINQ in order to do the comparison. 您可以使用Newtonsoft.JSON包反序列化JSON,然后使用LINQ遍历服务列表以进行比较。

Something like: 就像是:

class MicroserviceStatus {
   public string Name { get; set; }
   public string Status { get; set; }
   public string Description { get; set; }
}

class MicroserviceHealthCheck {
   public string Status { get; set; }
   public List<MicroserviceStatus> MicroserviceStatuses { get; set; }
}

var microserviceHealthCheck = JsonConvert.DeserializeObject<MicroserviceHealthCheck>(json);

bool anyNotHealthy = microserviceHealthCheck.MicroserviceStatuses.Any(i => i.Status != "Healthy");

I wrote this straight into the editor so it may not be exactly correct, and you should already have the types available for the deserialization already, but hopefully this helps. 我直接将其写到编辑器中,因此它可能不完全正确,并且您应该已经具有可用于反序列化的类型,但是希望这会有所帮助。

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

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