Doug Lerner
Doug Lerner

Reputation: 1533

Converting JSON strings with escaped Unicode characters to JavaScript objects

I have a JSON string which contains an escaped Unicode character. The JSON includes this snippet:

I co-ordinate our Chat Literacy network \u2013 an online group for practitioners of Information Literacy

The \u2013 is a long dash.

I'm using

var theObject = eval ("(" + jsonString + ")");

to convert the JSON string to a JavaScript object. I need to use a version of SpiderMonkey that doesn't have a direct JSON to Object method in it.

After conversion, the character in question becomes the Unicode control character \0013 which is an invalid UTF-8 character.

Is there another way I can convert the JSON to an object which will preserve the correct long-dash character? Maybe some other JSON to Object method I can load?

This happens with some other characters also, like curly quotes.

Thanks,

Doug

Upvotes: 1

Views: 1478

Answers (1)

AlexStack
AlexStack

Reputation: 17381

eval() is evil. Stay away from it. Try using JSON 3: http://bestiejs.github.io/json3/

Upvotes: 2

Related Questions