zoukankan      html  css  js  c++  java
  • json_encode 中文处理

    http://hi.baidu.com/life_to_you/item/ff7c70e12d270df32b09a453

    在 php 中使用 json_encode() 内置函数(php > 5.2)可以使用得 php 中数据可以与其它语言很好的传递并且使用它。

    这个函数的功能是将数值转换成json数据存储格式。


    <?php
    $arr= array
           (
              'Name'=>'希亚',
              'Age'=>20
           );
     
    $jsonencode= json_encode($arr);
    echo$jsonencode;
    ?>

    程序运行结果如下:


    {"Name":null,"Age":20}

    json_encode 函数中中文被编码成 null 了,Google 了一下,很简单,为了与前端紧密结合,Json 只支持 utf-8 编码,我认为是前端的 Javascript 也是 utf-8 的原因。


    <?php
    $array= array
        (
            'title'=>iconv('gb2312','utf-8','这里是中文标题'),
            'body'=>'abcd...'
        );
     
    echojson_encode($array);
    ?>

    这个程序的运行结果为:

    1{"title":"u8fd9u91ccu662fu4e2du6587u6807u9898","body":"abcd..."}

    数组中所有中文在json_encode之后都不见了或者出现u2353等。

    解决方法是用urlencode()函数处理以下,在json_encode之前,把所有数组内所有内容都用urlencode()处理一下,然用json_encode()转换成json字符串,最后再用urldecode()将编码过的中文转回来。

    <?php
        /**************************************************************
         *
         *  使用特定function对数组中所有元素做处理
         *  @param  string  &$array     要处理的字符串
         *  @param  string  $function   要执行的函数
         *  @return boolean $apply_to_keys_also     是否也应用到key上
         *  @access public
         *
         *************************************************************/
        function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
        {
            static $recursive_counter = 0;
            if (++$recursive_counter > 1000) {
                die('possible deep recursion attack');
            }
            foreach ($array as $key => $value) {
                if (is_array($value)) {
                    arrayRecursive($array[$key], $function, $apply_to_keys_also);
                } else {
                    $array[$key] = $function($value);
                }
          
                if ($apply_to_keys_also && is_string($key)) {
                    $new_key = $function($key);
                    if ($new_key != $key) {
                        $array[$new_key] = $array[$key];
                        unset($array[$key]);
                    }
                }
            }
            $recursive_counter--;
        }
          
        /**************************************************************
         *
         *  将数组转换为JSON字符串(兼容中文)
         *  @param  array   $array      要转换的数组
         *  @return string      转换得到的json字符串
         *  @access public
         *
         *************************************************************/
        function JSON($array) {
            arrayRecursive($array, 'urlencode', true);
            $json = json_encode($array);
            return urldecode($json);
        }
        
        $array = array
               (
                  'Name'=>'希亚',
                  'Age'=>20
               );
        
        
        echo JSON($array);
        ?>

    这次成功了,运行结果如下:


    {"Name":"希亚","Age":"20"}

  • 相关阅读:
    scrapy中selenium的应用
    Django的锁和事务
    redis
    【leetcode】187. Repeated DNA Sequences
    【leetcode】688. Knight Probability in Chessboard
    【leetcode】576. Out of Boundary Paths
    【leetcode】947. Most Stones Removed with Same Row or Column
    【leetcode】948. Bag of Tokens
    【leetcode】946. Validate Stack Sequences
    【leetcode】945. Minimum Increment to Make Array Unique
  • 原文地址:https://www.cnblogs.com/jevil/p/3586116.html
Copyright © 2011-2022 走看看