【问题标题】:Get array json data获取数组json数据
【发布时间】:2016-06-22 03:39:45
【问题描述】:

我将 Slim 框架用于 REST API。我的tasks 路由定义为:

$app->get('/tasks', 'authenticate', function() {
    global $user_id;
    $response = array();
    $items = array();
    $db = new DbHandler();
    $result = $db->getAllUserTasks($user_id);
    if ($result != NULL) {
        foreach ($result as $rez) {
            $response["error"] = false;
            $response["id"] = $rez["id"];
            $response["task"] = $rez["task"];
            $response["status"] = $rez["status"];
            $response["createdAt"] = $rez["created_at"];
            $items[] = $response;
        }
        echoRespnse(200, $items);  
    } else {
        $response["error"] = true;
        $response["message"] = "The requested resource doesn't exists";
        echoRespnse(404, $response);
    }              
});

我想要这种 json 响应:

{
"error": false,
"tasks": [
    {
        "id": 1,
        "task": "Complete test1",
        "status": 0,
        "createdAt": "2014-01-08 23:35:45"
    },
    {
        "id": 2,
        "task": "Complete test2",
        "status": 0,
        "createdAt": "2014-01-08 23:56:52"
    }
]
}

相反,我得到了类似的东西:

[1] 0: { error: false "2" task: "2" status: "2" createdAt: "2" }

我的路线有什么问题?

【问题讨论】:

    标签: php json rest httpresponse slim


    【解决方案1】:
    You need to format your array like below 
         if ($result != NULL) {
            $response["error"] = false;
            foreach ($result as $rez) { 
              $arr =array();
              $arr["id"] = $rez["id"];
              $arr["task"] = $rez["task"];
              $arr["status"] = $rez["status"];
              $arr["createdAt"] = $rez["created_at"];  
              $response['task'][] = $arr; 
            }
          }
         $item = $response;
    

    【讨论】:

    • 谢谢,但现在我得到这样的错误数据:[1] 0: { error: false task: [8] 0: { id: 11 }- 1: { task: "hhhhhhhhh" }- 2: { status: 0 }- 3: { createdAt: "2015-03-20 04:56:30" }- 4: { id: 12 }- 5: { task: "hhhhhhhhhnnnnnnn" }- 6: { status: 0 }- 7: { createdAt: "2015-03-20 04:57:50" }- - }
    • 同样的错误[1] 0: { error: false task: [26] 0: { id: 11 task: "hhhhhhhhh" status: 0 createdAt: "2015-03-20 04:56:30" }- 1: { id: 12 task: "hhhhhhhhhnnnnnnn" status: 0 createdAt: "2015-03-20 04:57:50" }- 2: { id: 13 task: "-7,643060" status: 0 createdAt: "2015-03-21 05:39:47" }
    • @David 您是否尝试查看写入响应的函数。也许问题就在那里。 echoResponse 函数有什么作用?
    【解决方案2】:

    你使用 php 函数 json_encode() 吗? 我认为您不是在创建 JSON 字符串,您可能只是在输出一个数组。

    <?php
    // your array with the JSON data
    // (associative array)
    // each array key is a json property and each array value is a json value.
    $arr =array();
    $arr["id"] = $rez["id"];
    $arr["task"] = $rez["task"];
    $arr["status"] = $rez["status"];
    $arr["createdAt"] = $rez["created_at"];  
    
    echo json_encode($arr); // this outputs a valid JSON string.
    

    也许你可以试试上面的代码。 而且我认为您当前的问题出在您的 echoRespnse 函数中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-02
      • 1970-01-01
      • 2013-02-19
      • 2021-11-08
      相关资源
      最近更新 更多