Patryk
Patryk

Reputation: 24092

How to parse special characters in json?

I am having problems with parsing special characters in javascript objects.

The escaped number makes the problem and double quotation character causes problems for me :

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
]

Upvotes: 1

Views: 4393

Answers (1)

GOTO 0
GOTO 0

Reputation: 47642

Your input strings are not correctly escaped for JavaScript. Use double backslashes to escape double quotation marks:

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

Upvotes: 1

Related Questions