2

I want to add JSON data with the following string value:

json = "d:\xyz\abc";

This value is coming 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.

Smi
  • 13,151
  • 9
  • 55
  • 63
Sachin J
  • 1,881
  • 12
  • 35
  • 49

4 Answers4

7

Escape it with another \:

var json = "d:\\xyz\\abc";
Andy E
  • 326,646
  • 82
  • 467
  • 441
4

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.

zed_0xff
  • 31,180
  • 7
  • 50
  • 72
0

Change to this:

json = "d:\\xyz\\abc";

See this question for further information

Community
  • 1
  • 1
gdoron is supporting Monica
  • 142,542
  • 55
  • 282
  • 355
0

\ 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."

J. Holmes
  • 18,226
  • 5
  • 44
  • 51