【问题标题】:How do I create a Dto in C# Asp.Net from a fairly complex Json Response如何在 C# Asp.Net 中从相当复杂的 Json 响应中创建 Dto
【发布时间】:2017-07-31 04:32:45
【问题描述】:

我已调用 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"
  }
}

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

例如,我将创建一个 Dto 类:

public class LeadDto {
  public string LeadId;
  public string Company;
}

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

我已经阅读https://github.com/restsharp/RestSharp/wiki/Deserialization 好久了,但没有得到任何结果。

谁能用例子指出我正确的方向?

【问题讨论】:

  • 您可以复制 JSON,转到 Visual Studio 并进行选择性粘贴。选择粘贴为 JSON。它将为您创建 C# 类。

标签: c# asp.net json


【解决方案1】:

复制 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; }
}

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

【讨论】:

    猜你喜欢
    • 2011-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-21
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多