简体   繁体   中英

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.

Below is the code I am using in the 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\\"}]"

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"}]

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.

So basically your are serializing the object to json string

[{"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"}]

and then serializing the json string to a serialized json string

"[{\\"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\\"}]"

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.

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

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