如何使PHP中的JSON输出更具人性化?

时间:2015-05-21 13:41:01

标签: php arrays json

我想要这个:

{

    "contacts": 

[

{

    "id": "c200",
    "name": "Ravi Tamada",
    "email": "ravi@gmail.com",
    "address": "xx-xx-xxxx,x - street, x - country",
    "gender": "male",
    "phone": 

    {
        "mobile": "+91 0000000000",
        "home": "00 000000",
        "office": "00 000000"
    }

},
{

    "id": "c201",
    "name": "Johnny Depp",
    "email": "johnny_depp@gmail.com",
    "address": "xx-xx-xxxx,x - street, x - country",
    "gender": "male",
    "phone": 

    {
        "mobile": "+91 0000000000",
        "home": "00 000000",
        "office": "00 000000"
    }

},
{

    "id": "c202",
    "name": "Leonardo Dicaprio",
    "email": "leonardo_dicaprio@gmail.com",
    "address": "xx-xx-xxxx,x - street, x - country",
    "gender": "male",
    "phone": 

    {
        "mobile": "+91 0000000000",
        "home": "00 000000",
        "office": "00 000000"
    }

},
{

    "id": "c203",
    "name": "John Wayne",
    "email": "john_wayne@gmail.com",
    "address": "xx-xx-xxxx,x - street, x - country",
    "gender": "male",
    "phone": 

    {
        "mobile": "+91 0000000000",
        "home": "00 000000",
        "office": "00 000000"
    }

},
{

    "id": "c204",
    "name": "Angelina Jolie",
    "email": "angelina_jolie@gmail.com",
    "address": "xx-xx-xxxx,x - street, x - country",
    "gender": "female",
    "phone": 

    {
        "mobile": "+91 0000000000",
        "home": "00 000000",
        "office": "00 000000"
    }

},
{

    "id": "c205",
    "name": "Dido",
    "email": "dido@gmail.com",
    "address": "xx-xx-xxxx,x - street, x - country",
    "gender": "female",
    "phone": 

    {
        "mobile": "+91 0000000000",
        "home": "00 000000",
        "office": "00 000000"
    }

},
{

    "id": "c206",
    "name": "Adele",
    "email": "adele@gmail.com",
    "address": "xx-xx-xxxx,x - street, x - country",
    "gender": "female",
    "phone": 

    {
        "mobile": "+91 0000000000",
        "home": "00 000000",
        "office": "00 000000"
    }

},
{

    "id": "c207",
    "name": "Hugh Jackman",
    "email": "hugh_jackman@gmail.com",
    "address": "xx-xx-xxxx,x - street, x - country",
    "gender": "male",
    "phone": 

    {
        "mobile": "+91 0000000000",
        "home": "00 000000",
        "office": "00 000000"
    }

},
{

    "id": "c208",
    "name": "Will Smith",
    "email": "will_smith@gmail.com",
    "address": "xx-xx-xxxx,x - street, x - country",
    "gender": "male",
    "phone": 

    {
        "mobile": "+91 0000000000",
        "home": "00 000000",
        "office": "00 000000"
    }

},
{

    "id": "c209",
    "name": "Clint Eastwood",
    "email": "clint_eastwood@gmail.com",
    "address": "xx-xx-xxxx,x - street, x - country",
    "gender": "male",
    "phone": 

    {
        "mobile": "+91 0000000000",
        "home": "00 000000",
        "office": "00 000000"
    }

},
{

    "id": "c2010",
    "name": "Barack Obama",
    "email": "barack_obama@gmail.com",
    "address": "xx-xx-xxxx,x - street, x - country",
    "gender": "male",
    "phone": 

    {
        "mobile": "+91 0000000000",
        "home": "00 000000",
        "office": "00 000000"
    }

},
{

    "id": "c2011",
    "name": "Kate Winslet",
    "email": "kate_winslet@gmail.com",
    "address": "xx-xx-xxxx,x - street, x - country",
    "gender": "female",
    "phone": 

    {
        "mobile": "+91 0000000000",
        "home": "00 000000",
        "office": "00 000000"
    }

},
{

    "id": "c2012",
    "name": "Eminem",
    "email": "eminem@gmail.com",
    "address": "xx-xx-xxxx,x - street, x - country",
    "gender": "male",
    "phone": 

            {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
        }
    ]

}

我的PHP返回:

[{"ContentID":"1","con_Model":"CAR_NAME","Picture_path":"localhost\/1.jpg"},{"ContentID":"2","con_Model":"CAR_NAME1","Picture_path":"localhost\/1.jpg"},{"ContentID":"1","con_Model":"CAR_NAME","Picture_path":"http:\/\/10.0.3.2\/1.jpg"}] 

和我的PHP代码

<?php
    //$array = array();
    require("config.inc.php");
    $query="SELECT Content.ContentID,Content.con_Model,Picture.Picture_path from Content INNER JOIN Picture ON Content.ContentID = Picture.ContentID LIMIT 25;";
        try {
            $stmt = $db->prepare($query);
            $stmt->execute();
            $array = $stmt->fetchAll(PDO::FETCH_ASSOC);
        }
        catch (PDOException $ex) {
            // For testing, you could use a die and message. 
            //die("Failed to run query: " . $ex->getMessage());

            //or just use this use this one to product JSON data:
            $response["success"] = 0;
            $response["message"] = "Database Error1. Please Try Again!";
            die(json_encode($response));

        }
        //$json_result = array("content" => $array);
            json_encode($array);
            print_r(json_encode($array));
?>

2 个答案:

答案 0 :(得分:1)

我认为您正在寻找JSON_PRETTY_PRINT选项。

Documentation for json_encode

示例:

echo '<pre>';
echo json_encode($array, JSON_PRETTY_PRINT);
echo '</pre>';

答案 1 :(得分:0)

此外,如果要自定义默认的JSON输出,一种方法是创建实现接口 \ JsonSerializable 的类。

这是一个小例子:

<?php
class Contact implements \JsonSerializable
{
    private $id;
    private $name;
    private $mail;

    public function __construct($id, $name, $mail) {
      $this->id = $id;
      $this->name = $name;
      $this->mail = $mail;
    }

    public function jsonSerialize() {
      return [
        'id' => $this->id,
        'name' => utf8_encode($this->name),
        'email' => utf8_encode($this->mail)
      ];
  }
}

$items = array();
$items[]=new Contact('c200', 'Ravi Tamada', 'ravi@gmail.com');
$items[]=new Contact('c201', 'Johnny Dep', 'johnny_depp@gmail.com');
$items[]=new Contact('c202', 'Leonardo Dicaprio', 'leonardo_dicaprio@gmail.com');

$response = array();
$response['contacts'] = $items;

echo json_encode($response , JSON_PRETTY_PRINT);

?>