Home > Article > Backend Development > When PHP outputs json data, an introduction to the method of not encoding unicode in Chinese
Today when writing the API interface, I converted the PHP array into json, but there is a rule that unicode encoding cannot be used
I tried several methods:
If PHP is 5.4 or above You can directly use the JSON_UNESCAPED_UNICODE parameter
json_encode('Chinese test',JSON_UNESCAPED_UNICODE);
This method is feasible for strings, but not for arrays (to be verified)
2 . Urlencode the string in the array, then convert the array json_encode into json, and finally urldecode
Note: urlencode() and urldecode() convert Chinese characters to hexadecimal and proceed according to certain rules String combination, realizes character encoding and decoding, and ensures the integrity and compatibility of characters during URL data transmission
The above method does avoid unicode encoding for Chinese, but when I finally tested the interface, it still failed If it doesn’t pass, just use normal json_encode to avoid misleading others. I will continue to test in the future...
The above is the detailed content of When PHP outputs json data, an introduction to the method of not encoding unicode in Chinese. For more information, please follow other related articles on the PHP Chinese website!