The question of json encoding is now being called by someone else. The development document says that the final returned data is in the json file format, and the Json format in the data must be UTF-8; do not contain Chinese or special characters in the data, which must be UTF-8 encoded. Data is stored in mssql. Can the following code be used in the final returned results to meet the above requirements? PHPcodehead json encoding problems
Now an interface is being called by someone else. The development documentation says that the final returned data is in the json file format, and the Json format must be UTF-8 for Chinese processing;
Do not contain Chinese or special characters in the data, which must be UTF-8 encoded.
Data is stored in mssql. Can the following code be used in the final returned results to meet the above requirements?
PHP code
header('Content-type: application/json;charset=utf-8');echo json_encode($data);
Or should I use the iconv function to convert and encode all fields that contain Chinese characters?
------ Solution --------------------
If you add a header, the file is UTF-8 without a bom header and is not converted. check how the database is stored.
Test the operation first and use iconv to convert the code. iconv also needs to know the original code, such as the database is gb2312 or gbk.
String iconv (string $ in_charset, string $ out_charset, string $ str)
------ Solution --------------------
Easy to get in and out. this is the consistent principle of programming and strictly abides by the requirements of the other party.
------ Solution --------------------
Mssql does not provide commands to dynamically change the language set.
You need to provide the required language set declaration (UTF-8) during connection)
In this way, the retrieved data is UTF-8 encoded.
Of course, this requires SQL server 2005 and above.
------ Solution --------------------
You only need to output your json file and call the data normally at the front end. if you cannot, iconv will be used in Chinese.
------ Solution --------------------
No matter where the data is obtained, you must know the encoding of the source data. if the source encoding is not utf8, you must transcode the data before the json Encoding. if the source data is already utf8, you do not need to process the json encoding before it is processed!
As long as the data is UTF-8 before json encoding, you can use it like this!
------ Solution --------------------
No ...... Json_encode will be converted into UCS-2 encoding ............