简体   繁体   English

如何从从 SQL Server 存储过程获取 JSON 结果的 C# Web API 返回 JSON 结果?

[英]How to return JSON result from C# Web API that gets JSON result from SQL Server Stored Procedure?

I have a Stored Procedure in SQL Server that returns the result as a JSON我在 SQL Server 中有一个存储过程,它以 JSON 形式返回结果

From the Web API, I call the Stored Procedure and store the results in an SQLDataReader.我从 Web API 调用存储过程并将结果存储在 SQLDataReader 中。

I now have to return the JSON result.我现在必须返回 JSON 结果。

SqlDataReader reader = cmd.ExecuteReader()

I have to convert the JSON string that the Stored Procedure returned in the reader as a JSON response to the API request我必须转换存储过程在读取器中返回的 JSON 字符串作为对 API 请求的 JSON 响应

I tried this我试过这个

 using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    DataTable dataTable = new DataTable();
                    dataTable.Load(reader);
                    return JsonConvert.SerializeObject(dataTable);
                }

The result I get is我得到的结果是

"[{\"JSON_F52E2B61-18A1-11d1-B105-00805F49916B\":\"[{\\\"ID\\\":2,\\\"StateCode\\\":42,\\\"CarrierID\\\":1,\\\"Code\\\":\\\"BI\\\",\\\"Type\\\":6,\\\"Name\\\":\\\"Bodily Injury\\\",\\\"Description\\\":\\\"Bodily Injury\\\",\\\"Priority\\\":2,\\\"IsActive\\\":true,\\\"EffectiveDate\\\":\\\"2016-11-03T00:00:00\\\",\\\"CreatedBy\\\":\\\"Admin\\\",\\\"CreatedDate\\\":\\\"2016-11-03T00:00:00\\\",\\\"ModifiedBy\\\":\\\"Admin\\\",\\\"ModifiedDate\\\":\\\"2016-11-03T00:00:00\\\"},{\\\"ID\\\":3,\\\"StateCode\\\":42,\\\"CarrierID\\\":1,\\\"Code\\\":\\\"PD\\\",\\\"Type\\\":6,\\\"Name\\\":\\\"Property Damage\\\",\\\"Description\\\":\\\"Property Damage\\\",\\\"Priority\\\":3,\\\"IsActive\\\":true,\\\"EffectiveDate\\\":\\\"2016-11-03T00:00:00\\\",\\\"CreatedBy\\\":\\\"Admin\\\",\\\"CreatedDate\\\":\\\"2016-11-03T00:00:00\\\",\\\"ModifiedBy\\\":\\\"Admin\\\",\\\"ModifiedDate\\\":\\\"2016-11-03T00:00:00\\\"},{\\\"ID\\\":4,\\\"StateCode\\\":42,\\\"CarrierID\\\":1,\\\"Code\\\":\\\"PIP\\\",\\\"Type\\\":6,\\\"Name\\\":\\\"Personal Injury Protection\\\",\\\"Description\\\":\\\"Personal Injury Protection\\\",\\\"Priority\\\":4,\\\"IsActive\\\":true,\\\"EffectiveDate\\\":\\\"2016-11-03T00:00:00\\\",\\\"CreatedBy\\\":\\\"Admin\\\",\\\"CreatedDate\\\":\\\"2016-11-03T00:00:00\\\",\\\"ModifiedBy\\\":\\\"Admin\\\",\\\"ModifiedDate\\\":\\\"2016-11-03T00:00:00\\\"},{\\\"ID\\\":5,\\\"StateCode\\\":42,\\\"CarrierID\\\":1,\\\"Code\\\":\\\"APIP\\\",\\\"Type\\\":6,\\\"Name\\\":\\\"Additional PIP\\\",\\\"Description\\\":\\\"Additional PIP\\\",\\\"Priority\\\":5,\\\"IsActive\\\":true,\\\"EffectiveDate\\\":\\\"2016-11-03T00:00:00\\\",\\\"CreatedBy\\\":\\\"Admin\\\",\\\"CreatedDate\\\":\\\"2016-11-03T00:00:00\\\",\\\"ModifiedBy\\\":\\\"Admin\\\",\\\"ModifiedDate\\\":\\\"2016-1

如果你的方法有一个返回类型ActionResult你总是可以做return Json(result, JsonRequestBehavior.AllowGet);

The JSON string you are getting from Stored Procedure can be Deserialize into object using Newtonsoft.Json like您从存储过程中获取的 JSON 字符串可以使用 Newtonsoft.Json 反序列化为对象,例如

    var obj = JsonConvert.DeserializeObject<Object>(jsonFromSp)

then return that object from API然后从 API 返回该对象

Have you tried converting your DataTable into a List of a custom object where the object's properties are the column names?您是否尝试将 DataTable 转换为自定义对象的列表,其中对象的属性是列名? Then you should be able to pass the JSON List?那么你应该能够传递JSON列表?

I found this solution worked for me,我发现这个解决方案对我有用,

                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    if (!reader.HasRows)
                    {
                        jsonResult.Append("");
                    }
                    else
                    {
                        while (reader.Read())
                        {
                            jsonResult.Append(reader.GetValue(0).ToString());
                        }
                    }
                    var response = this.Request.CreateResponse(HttpStatusCode.OK);
                    response.Content = new StringContent(jsonResult.ToString(), Encoding.UTF8, "application/json");
                    return response;
                }

Did not require any JSON Serialize or Deserialize.不需要任何 JSON 序列化或反序列化。 Plain and simple!干净利落! Thanks for the help guys.谢谢你们的帮助。

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

相关问题 如何从 SQL Server 存储过程调用嵌套的 json 到 C# 中的 API 调用 - How to call nested json from SQL Server stored procedure to API call in C# C# Web API 嵌套在存储过程中的 JSON - C# Web API Nested JSON from Stored Procedure in MSSQL 在 C# 中使用 Dapper 和 Postgresql 从存储过程中获取 json 或 jsonb 结果 - Geting result from json or jsonb result from stored procedure with Dapper and Postgresql in C# 从SQL Server存储过程向Web API C#返回自定义消息 - Return a custom message from SQL Server stored procedure to Web API C# 如何从C#中的SQL Server存储过程获取返回的结果 - How to get the result returned back from a SQL Server stored procedure in C# WEB API返回嵌套数组JSON结果C# - WEB API to return Nested Array JSON Result C# 从MVC web api中的存储过程返回json - Return in json from stored procedure in MVC web api 如何动态反序列化 C# 中 API 的 json 结果? - How to dynamically deserialize the json result from API in C#? 如何从存储过程中返回记录的集合并将结果存入C#代码中? - How to return collection of record from stored procedure and get result in to c# code? 如何从C#中的SQL存储过程中检索结果集和PRINT消息? - How can retrieve both a result set and a PRINT message from a SQL stored procedure in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM