I have a Sencha Touch app. One of the stores I have uses an ajax proxy and a json reader. Some of the strings in the JSON returned from my sinatra app occasionally contain this character: http://www.fileformat.info/info/unicode/char/2028/index.htm
Although it's invisible, the character occurs twice in the second string here, between the period and the ending quote:
"description": "Each of the levels requires logic, skill, and brute force to crush the enemy.
"
Try copy and pasting "Each of the levels requires logic, skill, and brute force to crush the enemy.
" into your javascript console! It won't be parsed as a string, and fails with SyntaxError: Unexpected token ILLEGAL
.
This causes the JSON response to fail. I've been stuck on this for a long time! Any suggestions?
In JSON the only characters you must escape are \, ", and control codes. Thus in order to escape your structure, you'll need a JSON specific function.
JSON data always uses the Unicode character set.
parse needs to get a string which consists only of unicode characters (see Json parsing with unicode characters). For you the JSON. parse method fails, because your string contains non-unicode characters.
The only reliable way to fix this is server-side. Make sure your JSON generator emits those characters escaped, e.g. as \u2028
.
In my experience, it's easiest to simply encode your JSON in plain ASCII which will always work. The downside is that it's less efficient as non-ASCII characters will take up more space, so depending on the frequency of those, you may not want that trade-off...
The documentation for Perl's JSON::XS has a good explanation of the problem and advice for how to fix it in Perl: http://search.cpan.org/perldoc?JSON::XS#JSON_and_ECMAscript
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With