php json中文处理方法,请json更懂中文

1、php5.3版本及以下。的处理方式

/**
     *php5.3版本以前,json中文问题的解决解决方案
     */
    function encode_json($str) {
        return urldecode(json_encode(url_encode($str)));
    }
    /**
     *php5.3版本以前,json中文问题的解决解决方案
     */
    function url_encode($str) {
        if(is_array($str)) {
            foreach($str as $key=>$value) {
                $str[urlencode($key)] = url_encode($value);
            }
        } else {
            $str = urlencode($str);
        }
    
        return $str;
    }

2、php5.4版本及以上的处理方式

function convert_string_value_to_utf8(&$value, &$key)
    {
    	if(is_string($value))
    	  $value=iconv('gbk', 'utf-8', $value); 	////IGNORE
    }


/*
     * 处理php5.4版本及以上,json中文问题的解决解决方案
    * */
    function json_encode_up($returnResult){
    	array_walk_recursive($returnResult, 'convert_string_value_to_utf8');
    	return json_encode($returnResult,JSON_UNESCAPED_UNICODE);
    }

  

原文地址:https://www.cnblogs.com/longhs/p/4475872.html