Home > Article > Backend Development > Methods to solve the problem of Chinese garbled JSON data in PHP
Methods to solve the Chinese garbled problem of JSON data in PHP
When developing using PHP, we often encounter the situation of outputting data in JSON format. However, sometimes garbled characters appear when the Chinese data is returned, which brings trouble to the display and data processing of the front-end page. This article will introduce some methods to solve the problem of Chinese garbled JSON data in PHP, hoping to help developers better deal with this common problem. Several commonly used solutions will be introduced in detail below, with specific code examples.
Method 1: Set the second parameter JSON_UNESCAPED_UNICODE when using the json_encode function
In PHP, when using the json_encode function to convert an array or object into a string in JSON format, you can set the second parameter parameter JSON_UNESCAPED_UNICODE to prevent Chinese characters from being escaped into Unicode encoding, resulting in garbled characters. The following is a sample code:
$data = array( 'name' => '张三', 'age' => 30 ); echo json_encode($data, JSON_UNESCAPED_UNICODE);
In this example, we convert the $data array into a JSON format string through json_encode, and set the JSON_UNESCAPED_UNICODE parameter so that Chinese characters will not be escaped and remain original Chinese characters.
Method 2: Use the header function to set the Content-Type to application/json
When returning JSON data, you can use the header function to set the Content-Type of the response to application/json and tell the browser The data returned is in JSON format. This helps the browser correctly parse the returned JSON data and avoid garbled characters. The following is a sample code:
$data = array( 'name' => '李四', 'age' => 25 ); header('Content-Type: application/json; charset=utf-8'); echo json_encode($data);
In this example, we first use the header function to set the Content-Type to application/json and specify the encoding to utf-8, and then use json_encode to convert the $data array to JSON The format string returned.
Method 3: Use the mb_convert_encoding function to convert the encoding
Sometimes, even if the JSON_UNESCAPED_UNICODE parameter is set or the Content-Type is set to application/json, garbled characters will still appear. At this time, you can try to use the mb_convert_encoding function to convert the data to UTF-8 encoding. The following is a sample code:
$data = array( 'name' => '王五', 'age' => 35 ); $jsonData = json_encode($data); $jsonData = mb_convert_encoding($jsonData, 'UTF-8', 'UTF-8'); echo $jsonData;
In this example, we first use json_encode to convert the data into a JSON format string, then convert it to UTF-8 encoding through mb_convert_encoding, and finally return it to the front-end page.
Summary
When you encounter the problem of Chinese garbled JSON data returned by PHP during development, you can try the above methods to solve it. By setting the JSON_UNESCAPED_UNICODE parameter, setting Content-Type to application/json, or using the mb_convert_encoding function to convert the encoding, you can effectively avoid the occurrence of Chinese garbled characters. I hope that the method introduced in this article can help developers who have similar problems, so that the characteristics of Chinese characters can be accurately retained during data transmission.
The above is the detailed content of Methods to solve the problem of Chinese garbled JSON data in PHP. For more information, please follow other related articles on the PHP Chinese website!