Home >Backend Development >PHP Problem >How to convert json unicode to Chinese in php

How to convert json unicode to Chinese in php

藏色散人
藏色散人Original
2023-01-19 09:54:482968browse

php method to convert json unicode to Chinese: 1. Use the "json_encode($log['result_data'],JSON_UNESCAPED_UNICODE);" method to convert; 2. Use "function unicodeDecode($unicode_str){.. .}" method to convert.

How to convert json unicode to Chinese in php

The operating environment of this tutorial: Windows 10 system, PHP version 8.1, DELL G3 computer

How to convert json unicode to Chinese in php ?

PHP converts unicode encoded json string to Chinese

Chinese in json is encoded

$s = '[{"param_name":"email","param_caption":"\u90ae\u7bb1","operator":"\u5305\u542b","value":"aaaa\u5927\u592b\u6492"}]';

Convert Chinese encoding to Chinese

Method 1.

json_encode($log['result_data'],JSON_UNESCAPED_UNICODE);

Method 2.

   /**
     * 把unicode编码的字符串转为人眼可看的字符串
     * @param $unicode_str
     *
     * @return string
     */
    function unicodeDecode($unicode_str){
        $unicode_str = str_replace('"', '\"', $unicode_str);
        $unicode_str = str_replace("'", "\'", $unicode_str);
        $json = '{"str":"'.$unicode_str.'"}';
 
        $arr = json_decode($json,true);
 
        if(empty($arr)){
            return '';
        }
 
        return $arr['str'];
    }

Result:

[{"param_name":"email","param_caption":"邮箱","operator":"包含","value":"aaaa大夫撒"}]

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to convert json unicode to Chinese in php. 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