【发布时间】:2012-08-10 09:04:33
【问题描述】:
我正在进行一个项目,必须使用 json 来实现我的目标。我正在使用 mysql 和使用 json_encode() 的 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));
}
【问题讨论】:
-
您的意思是要格式化 JSON?你有一个具体的原因吗?对于 JSON 解码器,这应该没关系...
-
是的,你想让它看起来漂亮的原因是什么。如果出于调试目的,您可以在许多浏览器上轻松使用
F12,弹出控制台-然后执行JSON.parse,然后将字符串粘贴到那里。
标签: php javascript mysql json