javascript - escape with JSON.parse() - Stack Overflow

I'm trying to apply a simple escaping process in javascript <-> php munication. I doesn'

I'm trying to apply a simple escaping process in javascript <-> php munication. I doesn't work... I'm not sure why.

here is some test code I wrote :

<head>
    <meta charset='utf-8'>
    <title>Test</title>
</head>

<body>
    <script>
        JSON.parse('[{"id":"43","english":"ar\"m","korean":"\ud314","date":"49 minute(s) ago."}]');
    </script>
</body>

</html>

The escaped character is a double quote inside the word arm. The error returned when opening the page is :

VM84:1 Uncaught SyntaxError: Unexpected token m in JSON at position 26
     at JSON.parse (<anonymous>)
    at test.html:11
(anonymous) @ test.html:11

I'm trying to apply a simple escaping process in javascript <-> php munication. I doesn't work... I'm not sure why.

here is some test code I wrote :

<head>
    <meta charset='utf-8'>
    <title>Test</title>
</head>

<body>
    <script>
        JSON.parse('[{"id":"43","english":"ar\"m","korean":"\ud314","date":"49 minute(s) ago."}]');
    </script>
</body>

</html>

The escaped character is a double quote inside the word arm. The error returned when opening the page is :

VM84:1 Uncaught SyntaxError: Unexpected token m in JSON at position 26
     at JSON.parse (<anonymous>)
    at test.html:11
(anonymous) @ test.html:11
Share Improve this question edited Feb 15, 2017 at 16:50 Dipiks 3,9282 gold badges26 silver badges39 bronze badges asked Feb 15, 2017 at 16:38 SylvainSylvain 4177 silver badges16 bronze badges 1
  • It's not clear what the point of this experiment is. If you're having problems with JSON sent by the server, then the problem is there anwyay. – Pointy Commented Feb 15, 2017 at 16:43
Add a ment  | 

1 Answer 1

Reset to default 6

When you include a JSON string as a string in JavaScript, you have to double the backslashes, because backslash is a metacharacter in both the JavaScript and the JSON syntaxes.

    JSON.parse('[{"id":"43","english":"ar\\\"m","korean":"\\ud314","date":"49 minute(s) ago."}]');

For the \" it's necessary to use three \ characters, so that when the JSON parser sees it you have \".

Note that it is not necessary to have those extra \ in the actual JSON that your server sends to the client (or that the client sends to the server).

Note that there are not too many good reasons for JSON text to be embedded in JavaScript source code as a JavaScript string constant. If you're building the JavaScript source as part of an HTML document using a server-side tool like PHP (or pretty much anything), JSON can be dropped into JavaScript source as an ordinary JavaScript object initializer, without an extra layer of quotes to make it look like a JavaScript string constant.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744245483a4564907.html

相关推荐

  • javascript - escape with JSON.parse() - Stack Overflow

    I'm trying to apply a simple escaping process in javascript <-> php munication. I doesn'

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信