c# - Single backslash in JSON serialized -


i need send string in format using mvc json serialization

"america\/new_york" 

but when create string

string timezone = @"america\/new_york";  return json(new { timezone = timezone }) 

in result get:

{ "timezone":"america\\/new_york" } 

how can it?

edited. if write @"america\/new_york"; error "unrecognized escape sequence" if write @"america/new_york"; "timezone":"america/new_york"

they same, json serializer adding escape character. if deserialize object, call obj, do;

   console.writeline(obj.timezone);    //outputs "america\/new_york" 

the same true whatever use display in view.

in c# @ before string literal precompiler directive not use escape characters. if remove @ have write "america\\/new_york" in order produce output "america\/new_york".

to clarify further;

 string = @"america\/new_york";  string b = "america\\/new_york";   == b //true   string c = "america\/new_york"; //compiler error because \/   // not recognized escape sequence 

list of valid escape sequences (for things tabs , new lines): http://msdn.microsoft.com/en-us/library/h21280bw.aspx

more info on using @ string literals: http://msdn.microsoft.com/en-us/library/aa691090(v=vs.71).aspx


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -