json往前台传输数据问题

在与yog传值的时候,只能传json,传其他的会报错。
昨天发现传json的时候,如果出现汉字还是会出错。
找到这段代码,解决掉:

 /** 
         * 将数组里面带有中文的字串保留以JSON格式返回 
         * 
         * @param   array $arr  数组 
         * @return  string JSON格式的字符串 
         */  
      
        public function toJson($arr)  
        {  
              
            $ajax = $this->ToUrlencode($arr);  
            $str_json = json_encode($ajax);  
            return urldecode($str_json);  
        }  
      
        /** 
         * 将数组里面带有中文的字串用urlencode转换格式返回 
         * 
         * @param   array $arr  数组 
         * @return  array 
         */  
        public function ToUrlencode($arr)  
        {  
      
            $temp = array();  
            if (is_array($arr))  
            {  
                foreach ($arr AS $key => $row)  
                {  
					$row=iconv("GB2312","UTF-8//IGNORE",$row); 
                    $temp[$key] = $row;  
                    if (is_array($temp[$key]))  
                    {  
                        $temp[$key] = ToUrlencode($temp[$key]);  
                    }  
                    else  
                    {  
                        $temp[$key] = urlencode($row);  
                    }  
                }  
            }  
            else  
            {  
                $temp = $arr;  
            }  
            return $temp;  
        }  

猜你喜欢

转载自blog.csdn.net/color_coral/article/details/73281586