Pretty-Printing JSON with PHP

后端 未结 24 1865
一向
一向 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 02:54

    Have color full output: Tiny Solution

    Code:

    $s = '{"access": {"token": {"issued_at": "2008-08-16T14:10:31.309353", "expires": "2008-08-17T14:10:31Z", "id": "MIICQgYJKoZIhvcIegeyJpc3N1ZWRfYXQiOiAi"}, "serviceCatalog": [], "user": {"username": "ajay", "roles_links": [], "id": "16452ca89", "roles": [], "name": "ajay"}}}';
    
    $crl = 0;
    $ss = false;
    echo "
    ";
    for($c=0; $c';
            else
                echo '';
        }
        echo $s[$c];
        if ( $s[$c] == '"' && $ss )
            echo '';
        if ( $s[$c] == '"' )
              $ss = !$ss;
        if ( $s[$c] == '{' || $s[$c] == '[' )
        {
            $crl++;
            echo "\n";
            echo str_repeat(' ', ($crl*2));
        }
    }
    echo $s[$c];
    

提交回复
热议问题