Cranialsurge Cranialsurge - 3 months ago 18
JSON Question

Incorrect URL format in JSON output via WCF service

I have a WCF service contract defined as follows:

[OperationContract]
[WebGet(
UriTemplate =
"HubContent/{language}?apptype={appType}"
,
ResponseFormat = WebMessageFormat.Json)]
HubResults GetHubContent(string language, string appType);


In the implmentation of this contract in the service, I return a List of objects that gets parsed out in JSON. However whenever one of the properties of the objects is a URL or any string that contains forward slashes the JSON that is returned by the service in the browser escapes the forward slashes. So this url:- http://www.xxx.com/test/site will look like this

http\/\/www.xxx.com\/test\/site.

Is there something I need to specify maybe in the contract related to formatting to rectify this ?

Answer

Escaping forward slashes is perfectly valid JSON (see http://json.org/ , the "string" construction). If you pass , for example,

{"myUrl":"http\/\/www.example.com\/test\/site"}

to any JSON parser (e.g. the JavaScript "eval" function), the value of myUrl will be "http://www.example.com/test/site" (it will automatically unescape the slashes).

Or did I misunderstand your question? In this case, can you post an example of JSON that you think is broken?