28

I am trying to return a JSON file using ASP.NET Web API (for testing).

public string[] Get()
{
    string[] text = System.IO.File.ReadAllLines(@"c:\data.json");

    return text;
}

In Fiddler this does appear as a Json type but when I debug in Chrome and view the object it appears as and array of individual lines (left). The right image is what the object should look like when I am using it.

Can anyone tell me what I should return to achieve a Json result in the correct format?

alt

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
ojhawkins
  • 3,200
  • 15
  • 50
  • 67
  • http://stackoverflow.com/questions/9847564/how-do-i-get-asp-net-web-api-to-return-json-instead-of-xml-using-chrome..May help you! – ssilas777 Mar 31 '13 at 03:55
  • @ssilas777 I don't think that's the same question. That's about returning XML vs. JSON as opposed to returning incorrect JSON. – Eilon Mar 31 '13 at 03:58

3 Answers3

31

Does the file already has valid JSON in it? If so, instead of calling File.ReadAllLines you should call File.ReadAllText and get it as a single string. Then you need to parse it as JSON so that Web API can re-serialize it.

public object Get()
{
    string allText = System.IO.File.ReadAllText(@"c:\data.json");

    object jsonObject = JsonConvert.DeserializeObject(allText);
    return jsonObject;
}

This will:

  1. Read the file as a string
  2. Parse it as a JSON object into a CLR object
  3. Return it to Web API so that it can be formatted as JSON (or XML, or whatever)
ojhawkins
  • 3,200
  • 15
  • 50
  • 67
Eilon
  • 25,582
  • 3
  • 84
  • 102
  • how would one do this if the .JSON file would be an uri and not on c:, the file does not accept an uri as input – Ewald Bos Jun 25 '18 at 19:28
23

I found another solution which works also if anyone was interested.

public HttpResponseMessage Get()
{
    var stream = new FileStream(@"c:\data.json", FileMode.Open);

    var result = Request.CreateResponse(HttpStatusCode.OK);
    result.Content = new StreamContent(stream);
    result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

    return result;
}
ojhawkins
  • 3,200
  • 15
  • 50
  • 67
  • 2
    The generic HttpResponseMessage is now deprecated as it wasn't Type safe.. http://stackoverflow.com/questions/10655350/returning-http-status-code-from-asp-net-mvc-4-web-api-controller – Markive May 08 '13 at 01:36
  • 4
    +1: The HttpResponseMessage may be deprecated, but it worked in the situation where the JSON property names were not valid CLR (e.g. had spaces in them). Your answer gave me the clue I needed to return generated raw text as JSON. Thanks – iCollect.it Ltd Jul 01 '13 at 07:34
5

I needed something similar, but IHttpActionResult (WebApi2) was required.

public virtual IHttpActionResult Get()
{
    var result = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.OK)
    {
        Content = new System.Net.Http.ByteArrayContent(System.IO.File.ReadAllBytes(@"c:\temp\some.json"))
    };

    result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
    return ResponseMessage(result);
}
Community
  • 1
  • 1
Alex Nolasco
  • 18,750
  • 9
  • 86
  • 81