WCF JSON Return Service should return Single Escape Sequence

What i am doing for changing html codes to JavaScript escapes is :

string input = "श्रीगंग..."

var output = Regex.Replace(input, @"&#([0-9]*);", 
               x => String.Format("\\u{0:X4}", int.Parse(x.Groups[1].Value)));
or alternately;

var output = String.Join("", WebUtility.HtmlDecode(input)
                   .Select(x => "\\u" + ((int)x).ToString("X4")));

But now i have a problem that when i am sending this data through wcf service in json format. and i am getting values accordingly. but only the output variable is going wrong which is in this format:

\\u0026\\u0023\\u0032\\u0033\\u0033\\u0037\\u003B\\u0026\\u0023\\u0032\\u0033\\u0037\\u0035\\u003B\\u0026\\u0023\\u0032\\u0033\\u0033\\u0038\\u003B\\u0026\\u0023\\u0032\\u0033\\u0036\\u0034\\u003B\\u0020\\u0026\\u0023\\u0032\\u0033\\u0032\\u0035\\u003B\\u0026\\u0023\\u0032\\u0033\\u0035

i don't want '\\u0026' but want only '\u0026' in response. i have tried everything like:

output.Replace("\\","'\'");
output.Replace("\\",@"\");

but didn.t work here.

Here is my WCF Service method:

 [OperationContract(Name = "GetByCity")]
        [WebInvoke(Method = "GET", UriTemplate = "/GetByCity/DeviceType={DeviceType}&CityId={id}&Limit={limit}", BodyStyle = WebMessageBodyStyle.WrappedRequest,
                    RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        NewsList GetByCity(string DeviceType, string id, string limit);

Now i don't know where to define utf-8 format.

Answers


The Unicode escpace sequence \u.... is used in C# source code and it's used in JSON formatted data but it's not used in C# string. C# strings fully support Unicode and have a direct representation for all Unicode characters.

If required in the transmitted JSON data, the WCF service will create the escape sequence automatically. But if the strings already contain these escape sequences, then the WCF service will add more escaping so the unnecessary escape sequences will be retained after the JSON data has been parsed.

So in order to fix your problem, you will need to replace the HTML entites (&#....;) with the real Unicode character, i.e. श needs to be replaced with the character having the Unicode code point 2358:

var output = Regex.Replace(input, @"&#([0-9]*);", 
           x => new String((char) int.Parse(x.Groups[1].Value), 1) );

Need Your Help

Return an empty promise

javascript jquery ajax typescript jquery-deferred

I have a function that returns a jQuery promise. It looks like this:

Show Fancybox once per session on a specific day

jquery fancybox

I'm trying to show a Fancybox on page load and only once per visit. So far so good...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.