I want to add JSON data with the following string value:
json = "d:\xyz\abc";
This value is ing from a database at runtime. When I am going to display it in datatable, JSON formatting error is displayed. My requirement is that the above value will be displayed as it is in the datatable. Please help.
I want to add JSON data with the following string value:
json = "d:\xyz\abc";
This value is ing from a database at runtime. When I am going to display it in datatable, JSON formatting error is displayed. My requirement is that the above value will be displayed as it is in the datatable. Please help.
Share Improve this question edited Jan 14, 2012 at 11:38 Smi 14.3k9 gold badges61 silver badges65 bronze badges asked Dec 13, 2011 at 12:02 Sachin JSachin J 2,20112 gold badges37 silver badges50 bronze badges 1- How are you creating your JSON? – deceze ♦ Commented Dec 13, 2011 at 12:04
4 Answers
Reset to default 7Escape it with another \
:
var json = "d:\\xyz\\abc";
You'd better use a JSON library for your programming language. You don't retrieve database values directly with jquery, aren't you?
So, you'd use something like JSON.escape(my_string_from_db)
, or, in Ruby language I usually do my_string.to_json
.
This will automatically escape everything that needs to be escaped.
Change to this:
json = "d:\\xyz\\abc";
See this question for further information
\
is the escape character in JavaScript strings, it gives special meaning to the character following the slash. Like \t
is a tab character, \n
is a new line. To put a backslash literal you'll need to use \\
The first backslash says the next character is going to be special, the following backslash says "oh, it's just a backslash."