带引号的JSON.parse字符串

mjs*_*lva 13 javascript json

我有这个:

JSON.parse('{"130.00000001":{"p_cod":"130.00000001","value":"130.00000001 HDD Upgrade to 2x 250GB HDD 2.5\" SATA2 7200rpm"}}');
Run Code Online (Sandbox Code Playgroud)

JSONLint说这是完全有效的json.但在执行时我有一个JSON.parse错误.

但是,如果我将我的代码更改为:

    JSON.parse('{"130.00000001":{"p_cod":"130.00000001","value":"130.00000001 HDD Upgrade to 2x 250GB HDD 2.5\\" SATA2 7200rpm"}}');
Run Code Online (Sandbox Code Playgroud)

(注意双反斜杠)

它有效,但现在JSONLint说invalid json.

有人可以帮助理解这种行为吗?

Dea*_*vey 19

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.

  • 得到它:str_replace('\\'','\\\\'',$ json)今天我了解到还有一个逃避的地狱:) (9认同)