开发者

Convert a JSON into a UTF-8 string

开发者 https://www.devze.com 2023-01-29 16:57 出处:网络
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 t开发者_JAVA百科he cha

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 t开发者_JAVA百科he characters. How do I do it?


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...

Which is incorrect. You must ensure that you wrap your json_encode with mysql_escape_string().

This will make sure that the data is put in MySQL as:

\u0644\u063a...

Then, when you use json_decode, it converts the HEX strings into UTF-8 and is output correctly.


You can try passing an option to json_encode():

json_encode ( $value, JSON_UNESCAPED_UNICODE );

The JSON_UNESCAPED_UNICODE option is only available in PHP version 5.4.0 and later.

Thanks.


You can't, in PHP. Besides, the strings will still be the same once you decode them.


you are looking exactly for the funcition json_decode it can convert json strings into utf8

here is an example of arabic word

$re = json_encode('لغة عربية'); 
echo $re ; 
$dd = json_decode($re);
echo $dd ; 
die;

it output : "\u0644\u063a\u0629 \u0639\u0631\u0628\u064a\u0629" لغة عربية

more examples here http://php.net/manual/en/function.json-decode.php

0

精彩评论

暂无评论...
验证码 换一张
取 消