简体   繁体   English

如何从 SQL Server 存储过程调用嵌套的 json 到 C# 中的 API 调用

[英]How to call nested json from SQL Server stored procedure to API call in C#

In my stored procedure, I am getting the right nested json, but when I call the api from Postman, the result is returned with slashes as shown below.在我的存储过程中,我得到了正确的嵌套 json,但是当我从 Postman 调用 api 时,结果以斜杠返回,如下所示。 How to remove these backslashes from my result?如何从我的结果中删除这些反斜杠? Is this approach correct, or should I go for some different approach?这种方法是否正确,还是我应该采用不同的方法?

The procedure is below程序如下

ALTER PROCEDIRE [dbo].[USP_MobileApp_MenuList]              
AS
BEGIN
    SELECT  
        MMM.userid AS userid,  
        MMM.type AS type,  
        MMM.pid AS pid,  
        mmm.pname AS pname,  
        mmm.url AS url,  
        Smenu = (SELECT  
                     MMS.sid AS sid,  
                     MMS.sname AS sname,  
                     MMS.icon AS icon,  
                     MMS.url AS url  
                 FROM M_MobileApp_SubMenu MMS 
                 WHERE MMS.pid = MMM.pid  
                 FOR JSON PATH)  
    FROM 
        M_MobileApp_Menu MMM) A  
END

The web Api call code is Web Api 调用代码是

[HttpGet]
[Route("getMobileMenulist")]
public HttpResponseMessage GetMobileMenuliste(HttpRequestMessage objrequest)
{
        DBdata objLogin = new DBdata();
        HttpResponseMessage respone = Request.CreateResponse(HttpStatusCode.OK, objLogin.GetMobileMenulist());
       
        return respone;
}

public object GetMobileMenulist()
{
        DataTable objData = new DataTable();

        Query = "USP_MobileApp_MenuList";

        try
        {
            using (ObjSqlConnection = new SqlConnection(MDMSConnectionString))
            {
                using (ObjSqlCommand = new SqlCommand(Query, ObjSqlConnection))
                {
                    ObjSqlCommand.CommandType = CommandType.StoredProcedure;

                    using (SqlDataAdapter sda = new SqlDataAdapter())
                    {
                        ObjSqlConnection.Open();
                        sda.SelectCommand = ObjSqlCommand;
                        sda.Fill(objData);

                        //for (int i = 0; i <= objData.Rows.Count - 1; i++)
                        //{
                        //    objData.Rows[i][1] = objData.Rows[i][1].ToString().Replace(@"\", "");
                        //}

                        ObjSqlConnection.Close();
                    }
                }

            }
            return objData;
        }
        catch (Exception ex)
        {
            Log = new Logger();
            Log.writeErrorLog(ex, 0, ex.Message);
            //objLog.LogError(ex);
        }
        finally
        {
            if (ObjSqlConnection.State != ConnectionState.Closed)
            {
                ObjSqlConnection.Close();
                ObjSqlConnection.Dispose();
            }
        }
        return objData;
}

The result I am getting in Postman:我在邮递员中得到的结果:

[

{
    "userid": 1,
    "type": "APP       ",
    "PID": 2,
    "PNAME": "Consumtion Log",
    "URL": "/ConsumtionLog",
    "smenu": "[{\"sid\":1,\"sname\":\"Comparison\",\"icon\":\"fa fa-shower\",\"url\":\"/ConsumtionLog/Comparison\"},{\"sid\":2,\"sname\":\"History\",\"url\":\"/ConsumtionLog/History\"}]"
},
{
    "userid": 1,
    "type": "APP       ",
    "PID": 3,
    "PNAME": "Billing",
    "URL": "/Billing",
    "smenu": "[{\"sid\":3,\"sname\":\"Pay my bill\",\"url\":\"/ Billing/Paymybill\"},{\"sid\":4,\"sname\":\"Billing History\",\"url\":\"/Billing/BillingHistory\"}]"
}]

The SQL Server has no native JSON type (or BJSON) and store the JSON as string. SQL Server 没有本机 JSON 类型(或 BJSON)并将 JSON 存储为字符串。 After read, just convert it to JSON:阅读后,只需将其转换为JSON:

let x = "[{\"sid\":1,\"sname\":\"Comparison\",\"icon\":\"fa fa-shower\",\"url\":\"/ConsumtionLog/Comparison\"},{\"sid\":2,\"sname\":\"History\",\"url\":\"/ConsumtionLog/History\"}]"

let y = JSON.parse(x)

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

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