Rumah  >  Artikel  >  pembangunan bahagian belakang  >  php JSON数据格式化(美化)的方法

php JSON数据格式化(美化)的方法

angryTom
angryTomke hadapan
2019-10-16 10:52:386626semak imbas

php JSON数据格式化(美化)的方法

一、使用 json_encode 输出

<?php
header(&#39;content-type:application/json;charset=utf8&#39;);
$arr = array(
    &#39;status&#39; => true,
    &#39;errMsg&#39; => &#39;&#39;,
    &#39;member&#39; =>array(
        array(
            &#39;name&#39; => &#39;李&#39;,
            &#39;gender&#39; => &#39;男&#39;
        ),
        array(
            &#39;name&#39; => &#39;赵&#39;,
            &#39;gender&#39; => &#39;女&#39;
        )
    )
);
  
echo json_encode($arr);
?>

 (推荐学习:PHP视频教程

输出:

{"status":true,"errMsg":"","member":[{"name":"\u674e","gender":"\u7537"},{"name":"\u8d75","gender":"\u5973"}]}

二、使用 jsonFormat 输出

<?php
  
/** Json数据格式化
* @param  Mixed  $data   数据
* @param  String $indent 缩进字符,默认4个空格
* @return JSON
*/
function jsonFormat($data, $indent=null){
  
    // 对数组中每个元素递归进行urlencode操作,保护中文字符
    array_walk_recursive($data, &#39;jsonFormatProtect&#39;);
  
    // json encode
    $data = json_encode($data);
  
    // 将urlencode的内容进行urldecode
    $data = urldecode($data);
  
    // 缩进处理
    $ret = &#39;&#39;;
    $pos = 0;
    $length = strlen($data);
    $indent = isset($indent)? $indent : &#39;    &#39;;
    $newline = "\n";
    $prevchar = &#39;&#39;;
    $outofquotes = true;
  
    for($i=0; $i<=$length; $i++){
  
        $char = substr($data, $i, 1);
  
        if($char==&#39;"&#39; && $prevchar!=&#39;\\&#39;){
            $outofquotes = !$outofquotes;
        }elseif(($char==&#39;}&#39; || $char==&#39;]&#39;) && $outofquotes){
            $ret .= $newline;
            $pos --;
            for($j=0; $j<$pos; $j++){
                $ret .= $indent;
            }
        }
  
        $ret .= $char;
         
        if(($char==&#39;,&#39; || $char==&#39;{&#39; || $char==&#39;[&#39;) && $outofquotes){
            $ret .= $newline;
            if($char==&#39;{&#39; || $char==&#39;[&#39;){
                $pos ++;
            }
  
            for($j=0; $j<$pos; $j++){
                $ret .= $indent;
            }
        }
  
        $prevchar = $char;
    }
  
    return $ret;
}
  
/** 将数组元素进行urlencode
* @param String $val
*/
function jsonFormatProtect(&$val){
    if($val!==true && $val!==false && $val!==null){
        $val = urlencode($val);
    }
}
  
header(&#39;content-type:application/json;charset=utf8&#39;);
  
$arr = array(
    &#39;status&#39; => true,
    &#39;errMsg&#39; => &#39;&#39;,
    &#39;member&#39; =>array(
        array(
            &#39;name&#39; => &#39;李&#39;,
            &#39;gender&#39; => &#39;男&#39;
        ),
        array(
            &#39;name&#39; => &#39;赵&#39;,
            &#39;gender&#39; => &#39;女&#39;
        )
    )
);
  
echo jsonFormat($arr);
  
?>

输出:

{
    "status":true,
    "errMsg":"",
    "member":[
        {
            "name":"李",
            "gender":"男"
        },
        {
            "name":"赵",
            "gender":"女"
        }
    ]
}

三、php5.4 以后,json_encode增加了JSON_UNESCAPED_UNICODE , JSON_PRETTY_PRINT 等几个常量参数。使显示中文与格式化更方便。

<?php
header(&#39;content-type:application/json;charset=utf8&#39;);   
$arr = array(  
    &#39;status&#39; => true,  
    &#39;errMsg&#39; => &#39;&#39;,  
    &#39;member&#39; =>array(  
        array(  
            &#39;name&#39; => &#39;李&#39;,  
            &#39;gender&#39; => &#39;男&#39;  
        ),  
        array(  
            &#39;name&#39; => &#39;赵&#39;,  
            &#39;gender&#39; => &#39;女&#39;  
        )  
    )  
);
echo json_encode($arr, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);

输出:

{
    "status": true,
    "errMsg": "",
    "member": [
        {
            "name": "李",
            "gender": "男"
        },
        {
            "name": "赵",
            "gender": "女"
        }
    ]
}

Atas ialah kandungan terperinci php JSON数据格式化(美化)的方法. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Artikel ini dikembalikan pada:www.zuopeng.gd.cn. Jika ada pelanggaran, sila hubungi admin@php.cn Padam