简体   繁体   中英

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. 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

[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. After read, just convert it to 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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