如何在 c# 中格式化 API 复杂模型

XuDong Peng-MSFT 10,176 信誉分 Microsoft 供应商
2024-02-02T06:07:39.66+00:00

大家好,

我想编写一个 Post API 来将数据存储在数据库中。 API 结构应与以下 Json 格式相同。

要求如下: 有一个组织有多个分支机构。 我想收集诸如组织、分支机构、每个分支机构的联系信息等信息。在分支机构中可能有多个联系人或没有联系人。 在这种情况下,我如何在 asp.net c# 中格式化我的 API 模型?

[
  {
    "dataType": "organization",
    "organizationData": {
      "organizationNPI": "int",
      "organizationName": "string",
      "organizationTIN": "string",
      "website": "string",
      "location": [
        {
          "addressLine1": "string",
          "addressLine2": "string",
          "city": "string",
          "state": "string",
          "contact": [
            {
              "firstName": "string",
              "lastName": "string",
              "middletName": "string",
              "email": "string",
              "phone": "string"
            },
            {
              "firstName": "string",
              "lastName": "string",
              "middletName": "string",
              "email": "string",
              "phone": "string"
            },
            {
              "firstName": "string",
              "lastName": "string",
              "middletName": "string",
              "email": "string",
              "phone": "string"
            }
          ]
        },
        {
          "addressLine1": "string",
          "addressLine2": "string",
          "city": "string",
          "state": "string"
        },
        {
          "addressLine1": "string",
          "addressLine2": "string",
          "city": "string",
          "state": "string"
        }
      ],
      "specialities": {
        "speciality1": "string",
        "speciality2": "string"
      },
      "languages": {
        "language1": "string",
        "language2": "string",
        "language3": "string"
      }
    }
  }
]

Note: 该案例整理于:How format API complex model in c#

ASP.NET API
ASP.NET API
ASP.NET: .NET Framework 中一套用于生成 Web 应用程序和 XML Web 服务的技术。API: 支持两个应用程序相互交互的软件中介。
7 个问题
0 个注释 无注释
{count} 票

接受的答案
  1. Zhi Lv - MSFT 32,021 信誉分 Microsoft 供应商
    2024-02-02T08:53:49.1466667+00:00

    你好,

    您是否要求 C# 结构来支持您提供的 JSON? 每个大括号代表一个映射到 C# 类的 JSON 对象。 方括号是 JSON 中的数组,因此它们映射到数组或 C# 中的“IEnumerable<T>”。 其他一切都只是简单的原始映射。

    public class Organization
            {
                public string dataType { get; set; }
                public OrganizationData organizationData { get; set; }
            }
    
            public class OrganizationData
            {
                public int organizationNPI { get; set; }
                public string organizationName { get; set; }
                public string organizationTIN { get; set; }
                public string website { get; set; }
                public IEnumerable<Location> location { get; set; }
    
            }
    
            public class Location
            {
                public string addressLine1 { get; set; }
                public string addressLine2 { get; set; }
                public string city { get; set; }
                public string state { get; set; }
    
                public IEnumerable<Contact> contact { get; set; }
            }
    
            public class Contact
            {
                public string firstName { get; set; }
                public string lastName { get; set; }
                public string middleName { get; set; }
                public string email { get; set; }
                public string phone { get; set; }
            }
    
    

    如果答案是正确的解决方案,请单击“接受答案”并请投赞成票。如果您对此答案有其他疑问,请点击“评论”。
    注意:如果您想接收此线程的相关电子邮件通知,请按照我们文档中的步骤启用电子邮件通知。
    最好的问候

    0 个注释 无注释

0 个其他答案

排序依据: 非常有帮助