Format echo json

This question already has an answer here:

  • Pretty-Printing JSON with PHP 20 answers

  • The first piece of advice I'd give is: Don't. JSON is a data format. Deal with it using tools rather then trying to have your server format it.

    If you are going to ignore that, then see the manual for the json_encode function where it gives a list of options which includes JSON_PRETTY_PRINT which is described as Use whitespace in returned data to format it. Available since PHP 5.4.0.

    Thus the steps are:

  • Make sure you are using PHP 5.4.0 or newer
  • json_encode($data, JSON_PRETTY_PRINT);

  • You can use json_encode($data, JSON_PRETTY_PRINT) in php 5.4+

    In php 5.3 & under that, you could try formatting it with regular expressions, but it's not too safe (or you could use library for encoding json).

    链接地址: http://www.djcxy.com/p/47270.html

    上一篇: 将JSON数据从一个行字符串表示为结构化来源

    下一篇: 格式化echo json