dongxian4531 2015-01-01 17:24
浏览 38

JSON编码格式错误

Sorry guys for asking this easy question, but I got a stuck in here.

I have a GET method from webservice to retrieve user data.

So I have the json output like this right now :

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

I got that output with this following code :

$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);
}

I do not know why my JSON output goes wrong like that. All I want is to get JSON output like this :

     {
        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"
           }
       ]
    }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 怎么把多于硬盘空间放到根目录下
    • ¥15 Matlab问题解答有两个问题
    • ¥50 Oracle Kubernetes服务器集群主节点无法访问,工作节点可以访问
    • ¥15 LCD12864中文显示
    • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
    • ¥15 gsoap生成onvif框架
    • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。
    • ¥15 stm32的can接口不能收发数据
    • ¥15 目标检测算法移植到arm开发板
    • ¥15 利用JD51设计温度报警系统