将JSON转换为UTF-8字符串 [英] Convert a JSON into a UTF-8 string
问题描述
我想将JSON对象转换为字符串.当我使用json_encode
时,我得到一个字符串,但全部带有十六进制字母.我想将其转换为UTF-8.换句话说,我想看角色.我该怎么办?
I want to convert a JSON object into a string. when I am using json_encode
I get a string but all with hex letters. I want to convert it to a UTF-8. In other words I want to see the characters. How do I do it?
推荐答案
我正在使用json_encode
将数据(例如阿拉伯字符)存储在MySQL字段中.
I was using json_encode
to store data such as Arabic Characters in MySQL fields.
它将像这样以十六进制形式存储阿拉伯字符:
It would store the Arabic characters as HEX within the Database like this:
u0644 u063a ...
u0644 u063a...
这是不正确的.您必须确保用mysql_escape_string()
包装json_encode
.
Which is incorrect. You must ensure that you wrap your json_encode
with mysql_escape_string()
.
这将确保将数据按以下方式放入MySQL:
This will make sure that the data is put in MySQL as:
\ u0644 \ u063a ...
\u0644\u063a...
然后,当您使用json_decode
时,它将十六进制字符串转换为UTF-8并正确输出.
Then, when you use json_decode
, it converts the HEX strings into UTF-8 and is output correctly.
这篇关于将JSON转换为UTF-8字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!