简体   繁体   English

C# Web API 嵌套在存储过程中的 JSON

[英]C# Web API Nested JSON from Stored Procedure in MSSQL

Overview:概述:

I'm building an API using ASP.NET Web API 2. I'm building Stored Procedures in SQL and linking these in the API to serve data. I'm building an API using ASP.NET Web API 2. I'm building Stored Procedures in SQL and linking these in the API to serve data.

I know that SQL can return JSON using FOR JSON AUTO , for example.例如,我知道 SQL 可以使用FOR JSON AUTO返回 JSON 。 But, I don't think it's the best place to configure the data and out put JSON .但是,我认为这不是配置数据和输出JSON的最佳位置。 So I'm assuming there must be a way to achieve nested JSON in the API.所以我假设必须有一种方法可以在 API 中实现嵌套的 JSON。

This is what I want to achieve:这就是我想要实现的目标:

    {
        "Apps": [
            {
                "ItemID": "1",
                "Path": "/AppReport",
                "Name": "AppReport",
                "Reports": [
                    {
                        "Reports_ItemID": "11",
                        "Reports_Path": "/AppReport/SubReport",
                        "Reports_Name": "SubReport"
                    },
                    {
                        "Reports_ItemID": "12",
                        "Reports_Path": "/AppReport/SubReport2",
                        "Reports_Name": "SubReport2"
                    }
                ]
            },
            {
                "ItemID": "2",
                "Path": "/AppReport2",
                "Name": "AppReport2",
                "Reports": [
                    {
                        "Reports_ItemID": "22",
                        "Reports_Path": "/AppReport/SubReport",
                        "Reports_Name": "SubReport"
                    }
                ]
            }
        ]
    }

At the moment it's coming out as a flat Array of Objects目前它以平面Objects Array的形式出现

    {
        "Apps": [
            {
                "ItemID": "1",
                "Path": "/AppReport",
                "Name": "AppReport",
                "Reports_ItemID": "11",
                "Reports_Path": "/AppReport/SubReport",
                "Reports_Name": "SubReport"
            },
            {
                "ItemID": "1",
                "Path": "/AppReport",
                "Name": "AppReport",
                "Reports_ItemID": "12",
                "Reports_Path": "/AppReport/SubReport2",
                "Reports_Name": "SubReport2"
            },

            ...
        ]
    }

I have a class of the Stored Procedure in the API that looks like this:我在 API 中有一个存储过程的 class,如下所示:

    namespace API.Stored_Procedures
    {
        public partial class SP_GetApps
        {
            public System.Guid ItemID { get; set; }
            public string Path { get; set; }
            public string Name { get; set; }
            public System.Guid Report_ItemID { get; set; }
            public string Report_Path { get; set; }
            public string Report_Name { get; set; }
        }

    }

My Controller looks like this:我的 Controller 看起来像这样:

    [HttpGet]
    [Route("{uid}", Name ="getReportApps")]
    [ResponseType(typeof(SP_GetApps_Result))]
    public IHttpActionResult SP_GetApps(string uid)
    {
        var res = db.Database.SqlQuery<SP_GetApps_Result>
            ("SP_GetApps {0}", uid);

        return Ok(res);
    }

In order to achieve the desired JSON Class should look like为了实现所需的 JSON Class 应该看起来像

public class App
{
    public string ItemID { get; set; }
    public string Path { get; set; }
    public string Name { get; set; }
    public List<Report> Reports { get; set; }
}

public class Report
{
    public string Reports_ItemID { get; set; }
    public string Reports_Path { get; set; }
    public string Reports_Name { get; set; }
}

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

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