最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to add "" character in JSON? - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

4 Answers 4

Reset to default 7

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

发布评论

评论列表(0)

  1. 暂无评论