带引号的JSON.parse字符串 [英] JSON.parse string with quotes
问题描述
我有这个:
JSON.parse('{"130.00000001":{"p_cod":"130.00000001","value":"130.00000001 HDD Upgrade to 2x 250GB HDD 2.5\" SATA2 7200rpm"}}');
JSONLint 说这是完全有效的json。但是在执行时我有一个 JSON .parse
错误。
JSONLint says it's perfectly valid json. But on execution I have a JSON.parse
error.
但是,如果我将我的代码更改为:
But, if I change my code to:
JSON.parse('{"130.00000001":{"p_cod":"130.00000001","value":"130.00000001 HDD Upgrade to 2x 250GB HDD 2.5\\" SATA2 7200rpm"}}');
(注意双反斜杠)
它有效,但现在JSONLint说无效的json
。
It works, but now JSONLint says invalid json
.
有人可以帮助理解这种行为吗?
Can someone help to understand this behavior?
推荐答案
这是有线格式之间的区别,以及您需要在代码中编写以获取有线格式的内容。当你在代码中声明这个时,你需要在你的文字中使用双-h,所以字符串得到一个反斜杠(否则它将解释\作为一个转义序列,只是声明一个并把它放在你的字符串中)。如果你打印出文字的值,你会看到一个反斜杠。
It's a difference between the wire format, and what you have to write in your code to get the wire format. When you declare this in code you need the double-\ in your literal so the string gets a single backslash (otherwise it will interpret \" as an escape sequence for just declaring a " and put that in your string). If you print out the value of the literal you will see a single backslash.
这篇关于带引号的JSON.parse字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!