【问题标题】:C# Web API Nested JSON from Stored Procedure in MSSQL来自 MSSQL 中存储过程的 C# Web API 嵌套 JSON
【发布时间】:2019-10-17 11:09:11
【问题描述】:

概述:

我正在使用 ASP.NET Web API 2 构建一个 API。我正在使用 SQL 构建存储过程并在 API 中链接这些以提供数据。

例如,我知道 SQL 可以使用 FOR JSON AUTO 返回 JSON。但是,我认为这不是配置数据和输出JSON 的最佳位置。所以我假设必须有一种方法可以在 API 中实现嵌套 JSON。

这就是我想要实现的目标:

    {
        "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"
                    }
                ]
            }
        ]
    }

目前它以Array of Objects 的形式推出

    {
        "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"
            },

            ...
        ]
    }

我在 API 中有一个存储过程类,如下所示:

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

    }

我的控制器如下所示:

    [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);
    }

【问题讨论】:

  • 你不能让你的方法返回 JsonResult ......比如 `return Ok(new JsonResult(res))' 并将 Report_itemId、Path 和 Name 作为另一个类,然后 SP_GetApps 将有列表一个新的类......这样你的控制会给你所有
  • @Veljko89 抱歉,我对 C# 不是很有经验,请您帮忙详细说明一下。

标签: c# asp.net json sql-server


【解决方案1】:

为了实现想要的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; }
}

【讨论】:

  • 是的,我创建了这部分,但不确定如何让存储过程将正确的位连接在一起
  • 如果您能够获取 App 类对象中的数据以及您想要获取其 JSON 的内容,那么您可以简单地使用 JavaScriptSerializer var data = new JavaScriptSerializer().Serialize(objDetails);
  • 如果你得到 JSON 格式的数据并想在 App 类对象中得到它,那么你可以使用 NewtonSoft.json.DeserializeObject 。喜欢App obj= JsonConvert.DeserializeObject&lt;App&gt;(yourJSONString);
  • 我想我在这之前有点 - 我没有数据要呈现给 App 类,我有来自上面的 SP_GetApps 中的数据,但我认为我需要操作数据到应用号?
  • 是的,我相信你需要操纵。等待其他有经验的小伙子回应,他们可能会为您提供更好的解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-10
  • 2020-12-11
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
相关资源
最近更新 更多