Pretty-Printing JSON with PHP

后端 未结 24 2120
一向
一向 2020-11-22 02:03

I\'m building a PHP script that feeds JSON data to another script. My script builds data into a large associative array, and then outputs the data using json_encode

24条回答
  •  遇见更好的自我
    2020-11-22 03:00

    You can modify Kendall Hopkins' answer a little in the switch statement to get a pretty clean looking and nicely indented printout by passing a json string into the following:

    function prettyPrint( $json ){
    
    $result = '';
    $level = 0;
    $in_quotes = false;
    $in_escape = false;
    $ends_line_level = NULL;
    $json_length = strlen( $json );
    
    for( $i = 0; $i < $json_length; $i++ ) {
        $char = $json[$i];
        $new_line_level = NULL;
        $post = "";
        if( $ends_line_level !== NULL ) {
            $new_line_level = $ends_line_level;
            $ends_line_level = NULL;
        }
        if ( $in_escape ) {
            $in_escape = false;
        } else if( $char === '"' ) {
            $in_quotes = !$in_quotes;
        } else if( ! $in_quotes ) {
            switch( $char ) {
                case '}': case ']':
                    $level--;
                    $ends_line_level = NULL;
                    $new_line_level = $level;
                    $char.="
    "; for($index=0;$index<$level-1;$index++){$char.="-----";} break; case '{': case '[': $level++; $char.="
    "; for($index=0;$index<$level;$index++){$char.="-----";} break; case ',': $ends_line_level = $level; $char.="
    "; for($index=0;$index<$level;$index++){$char.="-----";} break; case ':': $post = " "; break; case "\t": case "\n": case "\r": $char = ""; $ends_line_level = $new_line_level; $new_line_level = NULL; break; } } else if ( $char === '\\' ) { $in_escape = true; } if( $new_line_level !== NULL ) { $result .= "\n".str_repeat( "\t", $new_line_level ); } $result .= $char.$post; } echo "RESULTS ARE:

    $result"; return $result;

    }

    Now just run the function prettyPrint( $your_json_string ); inline in your php and enjoy the printout. If you're a minimalist and don't like brackets for some reason, you can get rid of those easily by replacing the $char.="
    ";
    with $char="
    ";
    in the top three switch cases on $char. Here's what you get for a google maps API call for the city of Calgary

    RESULTS ARE: 
    
    {
    - - - "results" : [
    - - -- - - {
    - - -- - -- - - "address_components" : [
    - - -- - -- - -- - - {
    - - -- - -- - -- - -- - - "long_name" : "Calgary"
    - - -- - -- - -- - -- - - "short_name" : "Calgary"
    - - -- - -- - -- - -- - - "types" : [
    - - -- - -- - -- - -- - -- - - "locality"
    - - -- - -- - -- - -- - -- - - "political" ]
    - - -- - -- - -- - - }
    - - -- - -- - -
    - - -- - -- - -- - - {
    - - -- - -- - -- - -- - - "long_name" : "Division No. 6"
    - - -- - -- - -- - -- - - "short_name" : "Division No. 6"
    - - -- - -- - -- - -- - - "types" : [
    - - -- - -- - -- - -- - -- - - "administrative_area_level_2"
    - - -- - -- - -- - -- - -- - - "political" ]
    - - -- - -- - -- - - }
    - - -- - -- - -
    - - -- - -- - -- - - {
    - - -- - -- - -- - -- - - "long_name" : "Alberta"
    - - -- - -- - -- - -- - - "short_name" : "AB"
    - - -- - -- - -- - -- - - "types" : [
    - - -- - -- - -- - -- - -- - - "administrative_area_level_1"
    - - -- - -- - -- - -- - -- - - "political" ]
    - - -- - -- - -- - - }
    - - -- - -- - -
    - - -- - -- - -- - - {
    - - -- - -- - -- - -- - - "long_name" : "Canada"
    - - -- - -- - -- - -- - - "short_name" : "CA"
    - - -- - -- - -- - -- - - "types" : [
    - - -- - -- - -- - -- - -- - - "country"
    - - -- - -- - -- - -- - -- - - "political" ]
    - - -- - -- - -- - - }
    - - -- - -- - - ]
    - - -- - -
    - - -- - -- - - "formatted_address" : "Calgary, AB, Canada"
    - - -- - -- - - "geometry" : {
    - - -- - -- - -- - - "bounds" : {
    - - -- - -- - -- - -- - - "northeast" : {
    - - -- - -- - -- - -- - -- - - "lat" : 51.18383
    - - -- - -- - -- - -- - -- - - "lng" : -113.8769511 }
    - - -- - -- - -- - -
    - - -- - -- - -- - -- - - "southwest" : {
    - - -- - -- - -- - -- - -- - - "lat" : 50.84240399999999
    - - -- - -- - -- - -- - -- - - "lng" : -114.27136 }
    - - -- - -- - -- - - }
    - - -- - -- - -
    - - -- - -- - -- - - "location" : {
    - - -- - -- - -- - -- - - "lat" : 51.0486151
    - - -- - -- - -- - -- - - "lng" : -114.0708459 }
    - - -- - -- - -
    - - -- - -- - -- - - "location_type" : "APPROXIMATE"
    - - -- - -- - -- - - "viewport" : {
    - - -- - -- - -- - -- - - "northeast" : {
    - - -- - -- - -- - -- - -- - - "lat" : 51.18383
    - - -- - -- - -- - -- - -- - - "lng" : -113.8769511 }
    - - -- - -- - -- - -
    - - -- - -- - -- - -- - - "southwest" : {
    - - -- - -- - -- - -- - -- - - "lat" : 50.84240399999999
    - - -- - -- - -- - -- - -- - - "lng" : -114.27136 }
    - - -- - -- - -- - - }
    - - -- - -- - - }
    - - -- - -
    - - -- - -- - - "place_id" : "ChIJ1T-EnwNwcVMROrZStrE7bSY"
    - - -- - -- - - "types" : [
    - - -- - -- - -- - - "locality"
    - - -- - -- - -- - - "political" ]
    - - -- - - }
    - - - ]
    
    - - - "status" : "OK" }
    

提交回复
热议问题