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,850
  • 9
  • 56
  • 64
Sachin J
  • 2,081
  • 12
  • 36
  • 50

4 Answers4

7

Escape it with another \:

var json = "d:\\xyz\\abc";
Andy E
  • 338,112
  • 86
  • 474
  • 445
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
  • 32,417
  • 7
  • 53
  • 72
0

Change to this:

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

See this question for further information

Community
  • 1
  • 1
gdoron
  • 147,333
  • 58
  • 291
  • 367
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,466
  • 5
  • 47
  • 52