Home > Backend Development > PHP Tutorial > Methods for formatting (beautifying) php JSON data

Methods for formatting (beautifying) php JSON data

angryTom
Release: 2023-04-07 17:12:02
forward
6728 people have browsed it

Methods for formatting (beautifying) php JSON data

一、使用 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);
?>
Copy after login

(推荐学习:PHP视频教程

输出:

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

二、使用 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);
  
?>
Copy after login

输出:

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

三、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);
Copy after login

输出:

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

The above is the detailed content of Methods for formatting (beautifying) php JSON data. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:www.zuopeng.gd.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