Home > php教程 > php手册 > body text

PHP json_encode输出josn格式并解决中文乱码

WBOY
Release: 2016-05-25 16:43:39
Original
1267 people have browsed it

json 数据格式数据用得最多的是与js,flash实时交互用的,那么php怎么返回json数据格式呢,下面我来分析一下实例.

PHP实例代码如下:

<?php
header(&#39;Content-type: text/json&#39;);
$fruits = array ( 
"fruits"  => array("a" => "orange", "b" => "banana", "c" => "apple"), 
"numbers" => array(1, 2, 3, 4, 5, 6), 
"holes"   => array("first", 5 => "second", "third") 
); 
echo json_encode($fruits);
?>
Copy after login

上面是英文是没有问题,如果是中文就会有问题,解决办法如下.

Json 只支持 utf-8 编码,我认为是前端的 Javascript 也是 utf-8 的原因,代码如下:

<?php
$array = array(
    &#39;title&#39; => iconv(&#39;gb2312&#39;, &#39;utf-8&#39;, &#39;这里是中文标题&#39;) ,
    &#39;body&#39; => &#39;abcd...&#39;
);
echo json_encode($array);
?>
Copy after login

//结果如下:

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

利用js来分析这个函数,代码如下:

$(function(){ 
 $(&#39;#send&#39;).click(function() { 
  $.getJSON(&#39;json.php&#39;, function(data) { 
  $(&#39;#resText&#39;).emptyempty(); 
   var html = &#39;&#39;; 
   $.each( data  , function(commentIndex, comment) { 
html += &#39;<div class="comment"><h6>&#39; + comment[&#39;username&#39;] + &#39;:</h6><p class="para">&#39; + comment[&#39;content&#39;] + &#39;</p></div>&#39;; 
   }) 
  $(&#39;#resText&#39;).html(html); 
 }) 
}) 
})
Copy after login

注意在你的php输出js格式时我们必须是header('Content-type: text/json');这样的头部信息发送,后面加一个完整的可解析中文乱码的问题程序,代码如下:

<?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(&#39;possible deep recursion attack&#39;);
    }
    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, &#39;urlencode&#39;, true);
    $json = json_encode($array);
    return urldecode($json);
}
$array = array(
    &#39;Name&#39; => &#39;希亚&#39;,
    &#39;Age&#39; => 20
);
echo JSON($array);
?>
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!