Pretty-Printing JSON with PHP

后端 未结 24 1867
一向
一向 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:53

    best way to format JSON data is like this!

    header('Content-type: application/json; charset=UTF-8');
    echo json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    

    Replace $response with your Data which is required to be converted to JSON

    0 讨论(0)
  • 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 "<pre>";
    for($c=0; $c<strlen($s); $c++)
    {
        if ( $s[$c] == '}' || $s[$c] == ']' )
        {
            $crl--;
            echo "\n";
            echo str_repeat(' ', ($crl*2));
        }
        if ( $s[$c] == '"' && ($s[$c-1] == ',' || $s[$c-2] == ',') )
        {
            echo "\n";
            echo str_repeat(' ', ($crl*2));
        }
        if ( $s[$c] == '"' && !$ss )
        {
            if ( $s[$c-1] == ':' || $s[$c-2] == ':' )
                echo '<span style="color:#0000ff;">';
            else
                echo '<span style="color:#ff0000;">';
        }
        echo $s[$c];
        if ( $s[$c] == '"' && $ss )
            echo '</span>';
        if ( $s[$c] == '"' )
              $ss = !$ss;
        if ( $s[$c] == '{' || $s[$c] == '[' )
        {
            $crl++;
            echo "\n";
            echo str_repeat(' ', ($crl*2));
        }
    }
    echo $s[$c];
    
    0 讨论(0)
  • 2020-11-22 02:55

    Use <pre> in combination with json_encode() and the JSON_PRETTY_PRINT option:

    <pre>
        <?php
        echo json_encode($dataArray, JSON_PRETTY_PRINT);
        ?>
    </pre>
    
    0 讨论(0)
  • 2020-11-22 02:56

    If you have existing JSON ($ugly_json)

    echo nl2br(str_replace(' ', '&nbsp;', (json_encode(json_decode($ugly_json), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES))));
    
    0 讨论(0)
  • 2020-11-22 02:57

    You could do it like below.

    $array = array(
       "a" => "apple",
       "b" => "banana",
       "c" => "catnip"
    );
    
    foreach ($array as $a_key => $a_val) {
       $json .= "\"{$a_key}\" : \"{$a_val}\",\n";
    }
    
    header('Content-Type: application/json');
    echo "{\n"  .rtrim($json, ",\n") . "\n}";
    

    Above would output kind of like Facebook.

    {
    "a" : "apple",
    "b" : "banana",
    "c" : "catnip"
    }
    
    0 讨论(0)
  • 2020-11-22 02:59

    The following is what worked for me:

    Contents of test.php:

    <html>
    <body>
    Testing JSON array output
      <pre>
      <?php
      $data = array('a'=>'apple', 'b'=>'banana', 'c'=>'catnip');
      // encode in json format 
      $data = json_encode($data);
    
      // json as single line
      echo "</br>Json as single line </br>";
      echo $data;
      // json as an array, formatted nicely
      echo "</br>Json as multiline array </br>";
      print_r(json_decode($data, true));
      ?>
      </pre>
    </body>
    </html>
    

    output:

    Testing JSON array output
    
    
    Json as single line 
    {"a":"apple","b":"banana","c":"catnip"}
    Json as multiline array 
    Array
    (
        [a] => apple
        [b] => banana
        [c] => catnip
    )
    

    Also note the use of "pre" tag in html.

    Hope that helps someone

    0 讨论(0)
提交回复
热议问题