Represent JSON data from one line string to structured source

This question already has an answer here:

  • Pretty-Printing JSON with PHP 20 answers

  • Use JSON_PRETTY_PRINT flag (PHP 5.4+):

    $json_str = json_encode($data, JSON_PRETTY_PRINT);
    

    Since you're having a JSON-string, you can first decode it into an object, and then re-encode it.

    Example:

    $str = '{"name":"John","age":"12","Location":"U.S.A"}';
    echo json_encode(json_decode($str), JSON_PRETTY_PRINT);
    

    Output:

    {
        "name": "John",
        "age": "12",
        "Location": "U.S.A"
    }
    

    Demo

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

    上一篇: 格式化json内容输出

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