【问题标题】:PHP - Problems with JSON encodingPHP - JSON 编码的问题
【发布时间】:2015-11-30 05:06:51
【问题描述】:

PHP - JSON 编码的问题

我目前正在开发一个 Android 应用程序,作为我学校项目的一部分。我需要设置一个将检索的 API 数据库中的所有讲座并将它们输出为 JSON。这是我希望如何输出它们的示例:

{
    "count: 4,
    "msg": "",
    "user_id": 1,
    "name": "John Doe"

    "lectures": [{
        "id": 1,
            "starting_at": "2015-11-30 13:00",
            "ending_at": "2015-11-30 15:00",
            "course_name": "Name of Course #1",
    }, {
        "id": 2,
            "starting_at": "2015-11-30 13:00",
            "ending_at": "2015-11-30 15:00",
            "course_name": "Name of Course #2",
    }, {
        "id": 3,
            "starting_at": "2015-11-30 13:00",
            "ending_at": "2015-11-30 15:00",
            "course_name": "Name of Course #3",
    }]
}

但相反,它复制了我检索到的所有对象,我的输出如下所示:

{
    "count":4,
    "msg":"",

    "lectures":[{
        "id":"1",
        "0":"1", // Duplicated object
        "starting_at": "2015-11-30 13:00:00",
        "1":"2015-11-30 13:00:00", // Duplicated object
        "ending_at":"2015-11-30 15:00:00",
        "2":"2015-11-30 15:00:00", // Duplicated object
        "user_id":"1",
        "3":"1", // Duplicate object
        "course":"Course Name #1",
        "4":"Course Name #1", // Duplicated object
        "user_name":"John Doe",
        "5":"John Doe"
    }, {
        "id":"2",
        "0":"2", // Duplicated object
        "starting_at": "2015-11-30 13:00:00",
        "1":"2015-11-30 13:00:00", // Duplicated object
        "ending_at":"2015-11-30 15:00:00",
        "2":"2015-11-30 15:00:00", // Duplicated object
        "user_id":"1",
        "3":"1", // Duplicated object
        "course": "Course Name #2",
        "4":"Course Name #2", // Duplicated object
        "user_name":"John Doe",
        "5":"John Doe" // Duplicated object
  ]}
}

我不得不承认我对 JSON 比较陌生,并且肯定需要一些帮助。这是我用于 API 调用的 PHP:

<?php
require_once("dbconnect.php");
$user_id = 1;

// Prepare the statement
$stmt = $pdo->prepare("
SELECT
lectures.id,
lectures.starting_at,
lectures.ending_at,
users_lectures.user_id,
courses.name AS course,
users.name AS user_name
FROM lectures
LEFT JOIN courses ON courses.id = lectures.course_id
LEFT JOIN users_lectures ON users_lectures.lecture_id = lectures.id
LEFT JOIN users ON users.id = users_lectures.user_id
WHERE users_lectures.user_id = :user_id
AND lectures.starting_at >= CURRENT_DATE()
ORDER BY lectures.starting_at DESC");

$stmt->execute(array(':user_id' => $user_id));
if($stmt->rowCount() > 0) {
    $lectures = array();

    while ($row = $stmt->fetch()) {    
        $lectures[] = $row;

    }

    echo json_encode(["count" => count($lectures), "msg" => "", "user_id" => $row['user_id'], "name" => $row['user_name'], "lectures" => [$lectures]]);
} else {
    echo json_encode(["count" => 0, "msg" => "No lectures today"]);
}

【问题讨论】:

    标签: php json


    【解决方案1】:

    这些不是重复的,它们是由于PDO's FETCH_MODE。默认情况下,它同时获取数字键和关联键。

    使用

    $stmt->fetch(PDO::FETCH_NUM) 
    

    对于数字和

    $stmt->fetch(PDO::FETCH_ASSOC)
    

    用于联想

    【讨论】:

      猜你喜欢
      • 2016-05-16
      • 2012-12-12
      • 1970-01-01
      • 1970-01-01
      • 2011-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多