[英]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.