php JSON資料格式化方法

來源:互聯網
上載者:User

php 的json_encode能把數群組轉換為json格式的字串。字串沒有縮排,中文會轉為unicode編碼,例如\u975a\u4ed4。人閱讀比較困難。現在這個方法在json_encode的基礎上再進行一次美化處理。使人能方便閱讀內容。


1. 使用 json_encode 輸出

<?phpheader('content-type:application/json;charset=utf8');$arr = array(    'status' => true,    'errMsg' => '',    'member' =>array(        array(            'name' => '李逍遙',            'gender' => '男'        ),        array(            'name' => '趙靈兒',            'gender' => '女'        )    ));echo json_encode($arr);?>
輸出:

{"status":true,"errMsg":"","member":[{"name":"\u674e\u900d\u9065","gender":"\u7537"},{"name":"\u8d75\u7075\u513f","gender":"\u5973"}]}
可以看出,這種格式人閱讀很困難。


2. 使用 jsonFormat 輸出

<?php/** Json資料格式化* @param  Mixed  $data   資料* @param  String $indent 縮排字元,預設4個空格* @return JSON*/function jsonFormat($data, $indent=null){    // 對數組中每個元素遞迴進行urlencode操作,保護中文字元    array_walk_recursive($data, 'jsonFormatProtect');    // json encode    $data = json_encode($data);    // 將urlencode的內容進行urldecode    $data = urldecode($data);    // 縮排處理    $ret = '';    $pos = 0;    $length = strlen($data);    $indent = isset($indent)? $indent : '    ';    $newline = "\n";    $prevchar = '';    $outofquotes = true;    for($i=0; $i<=$length; $i++){        $char = substr($data, $i, 1);        if($char=='"' && $prevchar!='\\'){            $outofquotes = !$outofquotes;        }elseif(($char=='}' || $char==']') && $outofquotes){            $ret .= $newline;            $pos --;            for($j=0; $j<$pos; $j++){                $ret .= $indent;            }        }        $ret .= $char;                if(($char==',' || $char=='{' || $char=='[') && $outofquotes){            $ret .= $newline;            if($char=='{' || $char=='['){                $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('content-type:application/json;charset=utf8');$arr = array(    'status' => true,    'errMsg' => '',    'member' =>array(        array(            'name' => '李逍遙',            'gender' => '男'        ),        array(            'name' => '趙靈兒',            'gender' => '女'        )    ));echo jsonFormat($arr);?>
輸出:

{    "status":true,    "errMsg":"",    "member":[        {            "name":"李逍遙",            "gender":"男"        },        {            "name":"趙靈兒",            "gender":"女"        }    ]}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.