javascript 带有撇号的 JSON 解析字符串(单引号)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33012552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 16:01:10  来源:igfitidea点击:

JSON parse string having Apostrophe (Single Quote)

javascript

提问by Muzafar Khan

How do i parse the following string

我如何解析以下字符串

var a = JSON.parse('[' + '{"NoteName":"it's my life","UserId":"100","NoteActive":true,"UserEmail":"[email protected]","CreatedDate":"8/13/2012 1:47:35 PM"}' + ']');

回答by alessandro

You have just to escape a single quote it\'s

你只需要转义单引号 it\'s

var a = JSON.parse('[' + '{"NoteName":"it\'s my life","UserId":"100","NoteActive":true,"UserEmail":"[email protected]","CreatedDate":"8/13/2012 1:47:35 PM"}' + ']');
console.log(a);

回答by LeFex

You can escape (interpret solely as characters) quote marks using backslash. "\"" or '\''

您可以使用反斜杠转义(仅解释为字符)引号。“\““ 或者 '\''

回答by Muhammad Usman

Replace it'swith it\'s

替换it'sit\'s

'[' + '{"NoteName":"it\'s my life","UserId":"100","NoteActive":true,"UserEmail":"[email protected]","CreatedDate":"8/13/2012 1:47:35 PM"}' + ']'