Retrieve JSON Data from SQL Server using a Stored Procedure and C#

4 Comments

  1. Thanks for this tuto.

    I have a problem :

    When I run my stored procedure in TSQL, it is fine :

    [{“pseudo”:”John”,”age”:85,”city”:”LYON”,”country”:”FR”},{“pseudo”:”Stève”,”age”:30,”city”:”NANTES”,”country”:”FR”}]

    But in c#, the result have escaped chars :

    “[{\”pseudo\”:\”John\”,\”age\”:85,\”city\”:\”LYON\”,\”country\”:\”FR\”},{\”pseudo\”:\”Steve\”,\”age\”:30,\”city\”:\”NANTES\”,\”country\”:\”FR\”}]”

    My code is basically the same as you..

  2. Author

    Hi Steve, sorry about my slow reply. Did you find an answer to your problem? Are you checking the variable contents in a breakpoint? If that is the case the escape characters are expected. If you output the JSON string to the console or use it elsewhere it should not contain the escape characters.

    1. I realize this is old, but in case anyone else wants to receive a JSON object rather than a string, this can be used if you’re returning a HttpResponseMessage object from your API:
      var response = req.CreateResponse(HttpStatusCode.OK);
      response.Content = new StringContent(jsonData, Encoding.UTF8, "application/json");
      return response;

      That will take whatever escaped json string data is in the jsonData variable and return it in the response as a json object.

Leave a Reply (Markdown formatting available)

This site uses Akismet to reduce spam. Learn how your comment data is processed.