如何解析json中的特殊字符

How to parse special characters in json?

本文关键字:特殊字符 json 何解析      更新时间:2024-04-13

我在解析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
]

您的输入字符串没有正确地为JavaScript转义。使用双反斜杠转义双引号:

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