对不起那些问这个简单问题的人,但我陷入了困境。
我有一个来自webservice的GET方法来检索用户数据。
所以我现在有这样的json输出:
{
error: false
users: [2]
0: {
user_id: 44
email: "asdasd1212@gmail.comnmmm"
name: "rendyyyyyyy"
phone: "1212121"
picture: "hahaha.jpg"
status: "Actived"
created_at: "2014-12-27 10:48:10"
}
-1: {
user_id: 45
email: "asdas1212151@gmail.comnmmm"
name: "rendyyyyyyy"
phone: "1212121"
picture: "hahaha.jpg"
status: "Actived"
created_at: "2014-12-27 10:48:15"
}
}
我使用以下代码获得了该输出:
$app->get('/admin/list/user', 'authenticate', function()
{
global $user_id;
$response = array();
// fetching all users
$userDB = new AdminController(MySqlDb::getInstance()->connect());
$users = $userDB->getAllUsers();
$response["error"] = false;
$response["users"] = array();
foreach ($users as $user) {
$tmp = array();
$tmp['user_id'] = $user->getUserId();
$tmp["email"] = $user->getEmail();
$tmp["name"] = $user->getName();
$tmp["phone"] = $user->getPhone();
$tmp["picture"] = $user->getPicture();
$tmp["status"] = $user->getStatus();
$tmp["created_at"] = $user->getCreatedAt();
array_push($response["users"], $tmp);
}
echoRespnse(200, $response);
});
function echoRespnse($status_code, $response)
{
$app = \Slim\Slim::getInstance();
// Http response code
$app->status($status_code);
// setting response content type to json
$app->contentType('application/json');
echo json_encode($response);
}
我不知道为什么我的JSON输出会出错。 我想要的就是获得这样的JSON输出:
{
error: false
users: [
{
user_id: 44
email: "asdasd1212@gmail.comnmmm"
name: "rendyyyyyyy"
phone: "1212121"
picture: "hahaha.jpg"
status: "Actived"
created_at: "2014-12-27 10:48:10"
},
{
user_id: 45
email: "asdas1212151@gmail.comnmmm"
name: "rendyyyyyyy"
phone: "1212121"
picture: "hahaha.jpg"
status: "Actived"
created_at: "2014-12-27 10:48:15"
}
]
}