Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning raw json (string) in wcf

Tags:

json

wcf

I want to build my own JSON, and have the service return a string, here is the code

[OperationContract] [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)] public string GetCurrentCart() {     //Code ommited     string jsonClient = null;     var j = new { Content = response.Content, Display=response.Display, SubTotal=response.SubTotal};     var s = new JavaScriptSerializer();     jsonClient = s.Serialize(j);     return jsonClient; } 

The response I am getting contains the \" used to create "'s in strings in c#.

The following is the response.

"{\"Content\":\"\\r\\n\\u003cdiv\\u003e\\r\\n\\u003cinput type=\\\"hidden\\\" name=\\\"__VIEWSTATE\\\" id=\\\"__VIEWSTATE\\\" value=\\\"\/wEPDwUBMA9kFgJmD2QWAmYPZBYGAgMPFgIeBFRleHQFKFlvdSBoYXZlIG5vIGl0ZW1zIGluIHlvdXIgc2hvcHBpbmcgY2FydC5kAgUPFgIeB1Zpc2libGVoZAIHDxQrAAIPFgIfAWhkZGQYAQUMY3RsMDEkbHZDYXJ0D2dkoWijqBUJaUxmDgFrkGdWUM0mLpgQmTOe8R8hc8bZco4=\\\" \/\\u003e\\r\\n\\u003c\/div\\u003e\\r\\n\\r\\n\\u003cdiv class=\\\"block block-shoppingcart\\\"\\u003e\\r\\n    \\u003cdiv class=\\\"title\\\"\\u003e\\r\\n        \\u003cspan\\u003eShopping Cart\\u003c\/span\\u003e\\r\\n    \\u003c\/div\\u003e\\r\\n    \\u003cdiv class=\\\"clear\\\"\\u003e\\r\\n    \\u003c\/div\\u003e\\r\\n    \\u003cdiv class=\\\"listbox\\\"\\u003e\\r\\n        You have no items in your shopping cart.\\r\\n        \\r\\n        \\r\\n    \\u003c\/div\\u003e\\r\\n\\u003c\/div\\u003e\\r\\n\",\"Display\":\"You have no items in your shopping cart.\",\"SubTotal\":null}" 

The values are being correctly encoded, but the json itself is not properly formatted. These \'s cause it to go out of wack.

How do I return a string without the \'s in front of the "'s?

like image 878
Paul Knopf Avatar asked Jun 20 '10 06:06

Paul Knopf


People also ask

How to return data in JSON format in WCF service?

Eliminate the code that you use to serialize the List to a json string - WCF does this for you automatically. WebInvoke with Method= "GET" is the same as WebGet, but since some of my methods are POST, I use all WebInvoke for consistency. The UriTemplate sets the URL at which the method is available.

Can WCF use JSON?

WCF supports serializing data in JSON format.

What is raw in JSON?

Raw JSON text is the format Minecraft uses to send and display rich text to players. It can also be sent by players themselves using commands and data packs. Raw JSON text is written in JSON, a human-readable data format.


2 Answers

Currently your web method return a String together with ResponseFormat = WebMessageFormat.Json. It follow to the JSON encoding of the string. Corresponds to www.json.org all double quotes in the string will be escaped using backslash. So you have currently double JSON encoding.

The easiest way to return any kind of data is to change the output type of GetCurrentCart() web method to Stream or Message (from System.ServiceModel.Channels) instead of String.
See http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-web.aspx, http://msdn.microsoft.com/en-us/library/ms789010.aspx and http://msdn.microsoft.com/en-us/library/cc681221(VS.90).aspx for code examples.

Because you don't wrote in your question which version of .NET you use, I suggest you to use an universal and the easiest way:

public Stream GetCurrentCart() {     //Code ommited     var j = new { Content = response.Content, Display=response.Display,                   SubTotal=response.SubTotal};     var s = new JavaScriptSerializer();     string jsonClient = s.Serialize(j);     WebOperationContext.Current.OutgoingResponse.ContentType =         "application/json; charset=utf-8";     return new MemoryStream(Encoding.UTF8.GetBytes(jsonClient)); } 
like image 189
Oleg Avatar answered Sep 20 '22 07:09

Oleg


I tried the method suggested by Oleg but found that when i used this method for a large amount of data null key word was appended at the end of the JSON string.

Example: for json with lots of data {"JsonExample":"xxxxx"}null

found a solution to address this problem at : http://wcf.codeplex.com/workitem/67 Wrote the following function which will accept a object and return a Pure Json output. So before returning my object in the main method i make a call to the below method.

  public HttpResponseMessage ReturnPureJson(object responseModel)     {         HttpResponseMessage response = new HttpResponseMessage();          string jsonClient = Json.Encode(responseModel);         byte[] resultBytes = Encoding.UTF8.GetBytes(jsonClient);         response.Content = new StreamContent(new MemoryStream(resultBytes));         response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");          return response;     } 
like image 38
Ivix4u Avatar answered Sep 21 '22 07:09

Ivix4u