javascript json 解析 - 转义引号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7179426/
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
json parse - unescaping the quotes
提问by André Al?ada Padez
so i am using json to pass some information from one place to another...
所以我使用 json 将一些信息从一个地方传递到另一个地方......
i have:
我有:
message.title = 'this is only a "test"';
so obviously, when i use JSON.stringify, i get escaped quotes.
很明显,当我使用 JSON.stringify 时,我得到了转义引号。
What i want to know is, what is the best way to unescape those quotes when using JSON.Parse. i have:
我想知道的是,在使用 JSON.Parse 时取消转义这些引号的最佳方法是什么。我有:
var message = JSON.parse(message);
var original = ????;
var final = ????;
var regex = new RegExp(original, 'g');
for(var prop in message){
message.data[prop] = message.data[prop].replace(regex, final);
}
I wish to know if i am doing something wrong and, as i tried various values in 'original' and 'final', what are correct values for them.
我想知道我是否做错了什么,当我尝试了“原始”和“最终”中的各种值时,它们的正确值是什么。
Thank you
谢谢
回答by Quentin
What i want to know is, what is the best way to unescape those quotes when using JSON.Parse
我想知道的是,在使用 JSON.Parse 时取消转义这些引号的最佳方法是什么
Do nothing. Parsing JSON will decode escapes. (If that doesn't work, then something is breaking between the data being converted to JSON and being parsed, or the data was bad to begin with)
没做什么。解析 JSON 将解码转义。(如果这不起作用,那么在转换为 JSON 的数据和被解析的数据之间会出现问题,或者数据一开始就不好)