Home >Backend Development >PHP Problem >How to make php json not encoded in Chinese

How to make php json not encoded in Chinese

藏色散人
藏色散人Original
2021-07-26 09:32:333130browse

php JSON Chinese non-encoding implementation method: 1. Urlencode the Chinese field, and then use urldecode; 2. Add the "JSON_UNESCAPED_UNICODE" option.

How to make php json not encoded in Chinese

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

php json is not encoded in Chinese?

When using the json_encode function that comes with PHP to encode data, the Chinese characters will become unicode, making it unreadable and increasing the size of the transmitted data. For example, for the string "I love boiled fish" "After performing json_encode, the output is "\u6211\u7231\u6c34\u716e\u9c7c", so how to prevent Chinese from being json_encoded and not encoded into unicode? There are two methods:

1. First convert the Chinese After field urlencode, json_encode, and then urldecode, Chinese can also be displayed.

echo urldecode(json_encode(urlencode("我爱水煮鱼")));

2. If you are using PHP version 5.4 or above, the json_encode function has added a new option: JSON_UNESCAPED_UNICODE. After adding this option, Chinese will not be automatically encoded.

echo json_encode("我爱水煮鱼", JSON_UNESCAPED_UNICODE);

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to make php json not encoded in Chinese. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn