JSON输出重音问题 [英] JSON output accents issue
问题描述
我一直在寻找3个小时,我知道有很多像我这样的问题已经被问到了,但是我没有找到任何解决方案,当我尝试输出时我的问题对我有用我的数据库上的SELECT查询对结果的重音字符。
I've been searching around for like 3 hours and I know there are a lot of questions like this one that have already been asked, but I haven't found any solution that works for me with my problem when trying to output accented characters of the result by a SELECT query on my database.
$itemsList = array();
while($row = mysql_fetch_assoc($res)){
$itemsList[Items][] = ($row);
}
echo json_encode($itemsList);
这将输出:
{"Items": [
{"user_name":"misterP",
"comment":"\u00c3\u00a9t\u00c3\u00a9",
"image_name":"403885300736874.jpg"}]}
但是我需要输出重音符号,如下所示:
But I need to output the accents, like this :
{"Items": [
{"user_name":"misterP",
"comment":"été",
"image_name":"403885300736874.jpg"}]}
- 我的数据库和表格有interclassement utf8_general_ci
- 我的数据库中插入的数据是utf8_encoded
- 我的php脚本是Unicode(UTF-8)
- 我做了mysql_set_charset( 'utf8');在我的数据库上的SELECT之前
- 如果这可以帮助某人:在数据库中,单词été是:été
- My database and tables have interclassement utf8_general_ci
- The datas inserted in my database are utf8_encoded
- my php script is in Unicode (UTF-8)
- I did " mysql_set_charset( 'utf8' ); " before the SELECT on my database
- If this can help someone : in y database, the word "été" is : "été"
所以我真的不知道现在该做什么,我真的很难受口音,我可以弄不明白工作。提前致谢 !
So I really don't know what to do now, I am really stuck with the accents and i can't get it work. Thanks in advance !
推荐答案
这并不重要。当json被解码时,它们会再次成为字符。
That doesn't really matter. When the json is decoded, they become characters again.
如果你使用php 5.4> =,你可以传递 JSON_UNESCAPED_UNICODE
到 json_encode
:
If you use php 5.4 >=, you can pass JSON_UNESCAPED_UNICODE
to json_encode
:
json_encode( $data, JSON_UNESCAPED_UNICODE )
但同样,unicode也能正常工作并使数据ascii安全。
But again, the unicode escapes work just as well and make the data ascii safe.
编辑:
看起来您的数据已被搞砸,甚至在 json_encode
。您已将其双重编码为utf-8。
It looks like your data was already screwed, even before json_encode
. You have "double encoded" it as utf-8 it seems.
我的数据库中插入的数据为utf8_encoded
The datas inserted in my database are utf8_encoded
您使用的是 utf8_encode
吗?插入数据库时,您无需在代码中执行任何操作。如果使用 utf8_encode
将数据插入数据库,那么这就解释了双重编码。所有你需要的是数据是utf-8并且在插入之前有 mysql_set_charset('utf8')
。
Are you using something like utf8_encode
? You do not need to do anything in your code when you insert into your database. If you inserted the data in the database using utf8_encode
, then that explains the double encoding. All you need is for the data to be utf-8 and have mysql_set_charset( 'utf8' )
before inserting.
有趣的是,如果你的json有未转义的unicode,json 不能即使您添加括号,如果json包含,也可以按javascript 执行 U + 2028
或 U + 2029
。
It is interesting to know that if your json has unescaped unicode, the json cannot be executed as javascript even if you add parentheses if the json contains U+2028
or U+2029
.
这篇关于JSON输出重音问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!