我正处于项目的中间,必须使用json来实现我的目标。我正在使用json_encode()使用mysql和php。我如何得到这个:
{"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));
}
答案 0 :(得分:2)
将JSON_PRETTY_PRINT
选项设置为名为on the manual page。
$output = json_encode(array('employee' => $employee), JSON_PRETTY_PRINT);
我不建议将其用于制作:它会增加文件大小。