Pretty-Printing JSON with PHP

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

    Many users suggested that you use

    echo json_encode($results, JSON_PRETTY_PRINT);
    

    Which is absolutely right. But it's not enough, the browser needs to understand the type of data, you can specify the header just before echo-ing the data back to the user.

    header('Content-Type: application/json');
    

    This will result in a well formatted output.

    Or, if you like extensions you can use JSONView for Chrome.

提交回复
热议问题