Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have this:

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

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"}}');

(note the double backslash)

It works, but now JSONLint says invalid json.

Can someone help to understand this behavior?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
122 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...