Home > Backend Development > PHP Tutorial > json解析解决方案

json解析解决方案

WBOY
Release: 2016-06-13 13:35:28
Original
855 people have browsed it

json解析
问一个问题啊 我已经解决json_encode 不能传中文的问题 在gbk编码的情况下 但是我怎么用json_decode()函数解析不出来啊

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
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);
    $json = urldecode($json);
    $json = str_replace("\"false\"","false",$json);
        $json = str_replace("\"true\"","true",$json);
        return $json;
    
}

function get_goodbook() {
       $sql = "select * from `wiki_doc`";
       $data=$this->db->getAll($sql);    
       return JSON($data);
    }


Copy after login


------解决方案--------------------
json_encode 可以传中文啊,传之前iconv就是了伐.
------解决方案--------------------
你把内容作了 url 编码,对方(比如js)拿到后如何知道需要解码呀?

你应该在 arrayRecursive 函数里做 iconv('gbk', 'utf-8', $value) 编码转换
------解决方案--------------------
一般要这么写,你自己再封装一下
PHP code
$ar = array (
  '这里是 GBK 编码的数据',
  array(
    '这是一个测试',
    '这还是一个测试',
  ),
);

echo $s = json_encode( array_map('gb2utf',$ar));
print_r(array_map('utf2gb', json_decode($s)));


function gb2utf($v) {
  if(! is_array($v)) return iconv('gbk', 'utf-8', $v);
  foreach($v as &$t) $t = gb2utf($t);
  return $v; 
}

function utf2gb($v) {
  if(! is_array($v)) return iconv('utf-8', 'gbk', $v);
  foreach($v as &$t) $t = utf2gb($t);
  return $v; 
}
<br><font color="#e78608">------解决方案--------------------</font><br>首先将数组转成utf-8的就可以编码中文了,没必要去写很多其它代码 <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template