JSON 格式问题


json formatting issues

我正在一个项目中,必须使用json来实现我的目标。我正在使用 mysql 和 php 使用 json_encode()。我如何得到这个:

{"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"}},

要长这个样子?

{
"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"
            }
    }
            ]
};

我知道这是可以做到的,因为多个教程在"回显"对象时让 json 对象看起来像这样,就像 http://api.androidhive.info/contacts/这个一样。我是否使用了错误的功能?这是我的代码。

$employee = array();
while($employee = mysql_fetch_array($result, MYSQL_ASSOC)) {
$employee[] = array('employee'=>$employee);
}
$output = json_encode(array('employee' => $employee));
}

按照手册页上的名称设置JSON_PRETTY_PRINT选项。

$output = json_encode(array('employee' => $employee), JSON_PRETTY_PRINT);

不过,我不建议将其用于生产:它会增加文件大小。