如何从相当复杂的Json Response中在C#Asp.Net中创建Dto

ozz*_*836 2 c# asp.net json

我已经调用了Api并使用RestSharp接收了此响应.我无法控制Json响应的结构.

{
  "response": {
    "result": {
      "Leads": {
        "row": [
          {
            "no": "1",
            "FL": [
              {
                "val": "LEADID",
                "content": "101"
              },
              {
                "val": "Company",
                "content": "Test 1"
              }
            ]
          },
          {
            "no": "2",
            "FL": [
              {
                "val": "LEADID",
                "content": "102"
              },
              {
                "val": "Company",
                "content": "Test 2"
              }
            ]
          }
        ]
      }
    },
    "uri": "/crm/private/json/Leads/getRecords"
  }
}
Run Code Online (Sandbox Code Playgroud)

我想在理想情况下从Json中提取一个Leads列表作为Dto,而不进行可怕的解析等.

所以例如我会创建一个Dto类:

public class LeadDto {
  public string LeadId;
  public string Company;
}
Run Code Online (Sandbox Code Playgroud)

这些潜在客户可以包含在列表或其他内容中.

我已经阅读了https://github.com/restsharp/RestSharp/wiki/Deserialization多年但没有到达任何地方.

任何人都能用实例指出我正确的方向吗?

Cod*_*shi 13

复制JSON,然后在Visual Studio中,在菜单栏中转到:编辑>选择性粘贴>将JSON粘贴为类:

在此输入图像描述

它将为您的JSON生成以下内容:

public class Rootobject {
   public Response response { get; set; }
}

public class Response {
   public Result result { get; set; }
   public string uri { get; set; }
}

public class Result {
   public Leads Leads { get; set; }
}

public class Leads {
   public Row[] row { get; set; }
}

public class Row {
   public string no { get; set; }
   public FL[] FL { get; set; }
}

public class FL {
   public string val { get; set; }
   public string content { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

您也可以通过选择"将XML粘贴为类"选项对XML执行相同操作.