最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - JSON data parser doesn't allow single-quote - Stack Overflow

programmeradmin0浏览0评论

I am writing "ments" from my website in my database with PHP using mysql_real_escape_string(). this will escape stuff out, as well as single-quote (') and double-quotes ("). when I load these ments back into my website, I'm using AJAX to load them, triggered by the scroll bar of a DIV to load 5 more on each scroll bar that reached bottom of DIV.

Everything works fine until an entry containes escaped \' (single-quote)

var ajax_entry_items = JSON.parse(http.responseText);

the JSON.parse function gives the error

Uncaught SyntaxError: Unexpected token ' in Google Chrome.

How can I still input single-quotes and double-quotes in my ments in my database and bring them back to display them on the website ? The double-quotes and single-quotes e back escaped but for some reason the single-quote, escaped, generates the

Unexpected token" error.

Thanks.

I am writing "ments" from my website in my database with PHP using mysql_real_escape_string(). this will escape stuff out, as well as single-quote (') and double-quotes ("). when I load these ments back into my website, I'm using AJAX to load them, triggered by the scroll bar of a DIV to load 5 more on each scroll bar that reached bottom of DIV.

Everything works fine until an entry containes escaped \' (single-quote)

var ajax_entry_items = JSON.parse(http.responseText);

the JSON.parse function gives the error

Uncaught SyntaxError: Unexpected token ' in Google Chrome.

How can I still input single-quotes and double-quotes in my ments in my database and bring them back to display them on the website ? The double-quotes and single-quotes e back escaped but for some reason the single-quote, escaped, generates the

Unexpected token" error.

Thanks.

Share Improve this question edited Jun 3, 2012 at 12:30 Joseph 120k30 gold badges184 silver badges237 bronze badges asked Jun 3, 2012 at 12:27 Adrian TanaseAdrian Tanase 7008 silver badges21 bronze badges 1
  • Run your text through the JSON validator. It can help in debugging issues like this. jsonformatter.curiousconcept. – Brandon J. Boone Commented Jun 3, 2012 at 12:30
Add a ment  | 

2 Answers 2

Reset to default 7

It is not allowed to enclose strings in single quotes, nor to escape single quotes, in JSON. You must escape double quotes and must not escape single quotes.

You should encode the data you're outputting from the database with json_encode():

echo json_encode($data);
发布评论

评论列表(0)

  1. 暂无评论