1

I have a json stored in database in a ntext field, I want to return that in a svc (c#) webservice , webservice is get and response type is string such as below :

    [WebInvoke(Method = "GET",
       ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "gcwbcc/{cityCode}/{key}")]

but in result i get extra double qutation also all double qutions in json converts to \" then I have two problems one is extra double qutation , and second is convertint " to \" in result.

ali kamrani
  • 73
  • 1
  • 8
  • That is a escaped JSON string. `{ "prop" : "value" }` will become `"{\"prop\":\"value\"}"` seems that the code is treating the json stored as just a simple string, you may have to parse it and return an object and then return it, or maybe you can send it as a raw response and then it wont get escaped. – KinSlayerUY Dec 13 '17 at 13:26
  • hi, thanks for adding comment, is there any build in object which can I cast string to it . I test webservice with a single string "a" and " shows in response, I want the response just be a not "a" – ali kamrani Dec 13 '17 at 13:39

1 Answers1

1

I solve it by changing return type of web method to stream and with writing this simple line of code:

byte[] resultBytes = Encoding.UTF8.GetBytes(result); WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8"; return new MemoryStream(resultBytes);

refrence: Returning raw json (string) in wcf but here is original guide https://blogs.msdn.microsoft.com/carlosfigueira/2008/04/17/wcf-raw-programming-model-web/

ali kamrani
  • 73
  • 1
  • 8