简体   繁体   English

ASP .NET Web API返回JSON文件

[英]ASP .NET Web API Returning JSON file

I am creating a ASP.NET Web API service end point where it queries the Oracle Database and returns the result in JSON format. 我正在创建一个ASP.NET Web API服务端点,在该端点查询Oracle数据库并以JSON格式返回结果。

Below is the code I am using in the Controller 以下是我在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;
 }
}

Everything works fine, except that it returns result is in the below format 一切正常,但返回的结果格式如下

"[{\\"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 \\“:\\”内部学习\\“,\\” 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 \\ “}]”

It is just that it creates the extra quotes and the extra escaping characters ( \\ ). 只是它会创建多余的引号和多余的转义字符( \\ )。 Do I need to do the manipulation to have them removed. 我是否需要进行操作以将其删除。

[{"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”:“内部学习”,“ 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”}]

That is because you are already serializing the data to JSON and then trying to return it as application/json which only results in the formatter converting your string into a JSON formatted string. 那是因为您已经将数据序列化为JSON,然后尝试将其作为application/json ,这只会导致格式化程序将您的字符串转换为JSON格式的字符串。

So basically your are serializing the object to json string 所以基本上您正在将对象序列化为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"}] [{{“ CATEGORY”:“内部学习”,“ 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”}]

and then serializing the json string to a serialized json string 然后将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\\"}]" “ [{\\” CATEGORY \\“:\\”内部学习\\“,\\” 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 \\ “}]”

Remove.. 去掉..

string result = JsonConvert.SerializeObject(selectResults);

... and just pass the selectResults object as it is to the response and the formatter will do the rest based on the media type. ...,然后将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;

If for example you changed the medial type to xml the response will be returned as xml. 例如,如果您将中介类型更改为xml,则响应将作为xml返回。

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

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