如何解析json中的特殊字符?

时间:2013-12-07 13:17:50

标签: javascript json escaping special-characters

我在解析javascript对象中的特殊字符时遇到问题。

转义的数字会导致问题和双引号字符对我造成问题:

JSON.parse('[{"title":"Turpial 3 Beta Builds Available for Ubuntu","description":"<p><img width=\"350"}]');
SyntaxError: Unexpected number
message: "Unexpected number"
stack: (...)
get stack: function () { [native code] }
set stack: function () { [native code] }
__proto__: Error

JSON.parse('[{"title":"Turpial 3 Beta Builds Available for Ubuntu","description":"<p><img width=\""}]');
SyntaxError: Unexpected string
message: "Unexpected string"
stack: (...)
get stack: function () { [native code] }
set stack: function () { [native code] }
__proto__: Error

JSON.parse('[{"title":"Turpial 3 Beta Builds Available for Ubuntu","description":"<p><img width="}]');
[
Object
]

1 个答案:

答案 0 :(得分:1)

您的输入字符串未针对JavaScript正确转义。使用双反斜杠来转义双引号:

JSON.parse('[{"title":"Turpial 3 Beta Builds Available for Ubuntu","description":"<p><img width=\\"350"}]');