gpt4 book ai didi

c# - ASP .NET Web API 返回 JSON 文件

转载 作者:行者123 更新时间:2023-11-30 21:47:11 25 4
gpt4 key购买 nike

我正在创建一个 ASP.NET Web API 服务端点,它在其中查询 Oracle 数据库并以 JSON 格式返回结果。

下面是我在 Controller 中使用的代码

public class SampleController : ApiController
{
public HttpResponseMessage Getdetails([FromUri] string[] id)
{
using (OracleConnection dbconn = new OracleConnection("DATA SOURCE=J;PASSWORD=C;PERSIST SECURITY INFO=True;USER ID=T"))
{
var inconditions = id.Distinct().ToArray();
var srtcon = string.Join(",", inconditions);
DataSet userDataset = new DataSet();
var strQuery = @"SELECT * from STCD_PRIO_CATEGORY where STPR_STUDY.STD_REF IN(" + srtcon + ")";
OracleCommand selectCommand = new OracleCommand(strQuery, dbconn);
OracleDataAdapter adapter = new OracleDataAdapter(selectCommand);
DataTable selectResults = new DataTable();
adapter.Fill(selectResults);
string result = JsonConvert.SerializeObject(selectResults);
string contentDisposition = "inline; filename=ProvantisStudyData.json";
//byte[] byteInfo = Encoding.ASCII.GetBytes(result);
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, result, MediaTypeHeaderValue.Parse("application/json"));
response.Content.Headers.ContentDisposition = ContentDispositionHeaderValue.Parse(contentDisposition);
//response.Content.Headers.ContentLength = byteInfo.Length;
return response;
}
}

一切正常,除了它返回的结果格式如下

"[{\"CATEGORY\":\"Internal Study\",\"SESSION_NUMBER\":7,\"SESSION_START_DATE\":\"2015-02-13T00:00:00\",\"SESSION_START_TIME\":\"2015-02-13T10:33:59.288394\",\"SESSION_END_DATE\":\"2015-02-13T00:00:00\",\"SESSION_END_TIME\":\"2015-02-13T12:11:34\"}]"

只是它创建了额外的引号和额外的转义字符 (\)。我是否需要进行操作才能将它们移除。

[{"CATEGORY":"Internal Study","SESSION_NUMBER":7,"SESSION_START_DATE":"2015-02-13T00:00:00","SESSION_START_TIME":"2015-02-13T10:33:59.288394","SESSION_END_DATE":"2015-02-13T00:00:00","SESSION_END_TIME":"2015-02-13T12:11:34"}]

最佳答案

那是因为您已经将数据序列化为 JSON,然后尝试将其作为 application/json 返回,这只会导致格式化程序将您的字符串转换为 JSON 格式的字符串。

所以基本上你是将对象序列化为 json 字符串

[{"CATEGORY":"Internal Study","SESSION_NUMBER":7,"SESSION_START_DATE":"2015-02-13T00:00:00","SESSION_START_TIME":"2015-02-13T10:33:59.288394","SESSION_END_DATE":"2015-02-13T00:00:00","SESSION_END_TIME":"2015-02-13T12:11:34"}]

然后将json串序列化为序列化后的json串

"[{\"CATEGORY\":\"Internal Study\",\"SESSION_NUMBER\":7,\"SESSION_START_DATE\":\"2015-02-13T00:00:00\",\"SESSION_START_TIME\":\"2015-02-13T10:33:59.288394\",\"SESSION_END_DATE\":\"2015-02-13T00:00:00\",\"SESSION_END_TIME\":\"2015-02-13T12:11:34\"}]"

删除..

string result = JsonConvert.SerializeObject(selectResults);

... 只需将 selectResults 对象原样传递给响应,格式化程序将根据媒体类型完成其余工作。

//...other code removed for brevity

var response = Request.CreateResponse(HttpStatusCode.OK, selectResults, MediaTypeHeaderValue.Parse("application/json"));
ContentDispositionHeaderValue contentDisposition = null;
if (ContentDispositionHeaderValue.TryParse("inline; filename=ProvantisStudyData.json", out contentDisposition)) {
response.Content.Headers.ContentDisposition = contentDisposition;
}
return response;

例如,如果您将媒体类型更改为 xml,则响应将作为 xml 返回。

关于c# - ASP .NET Web API 返回 JSON 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38668304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com