如何返回有效的JSON对象?

时间:2016-01-30 23:33:55

标签: php json

我刚刚进入php,我遇到了返回JSON对象的问题。

以下是代码:

$stmt->bind_result($image_link, $start_time, $end_time, $viet_performer, $english_performer, $viet_event, $english_event, $day, $stage);

            while($stmt->fetch())
            {
                echo json_encode([["image_link" => $image_link,"start_time" => $start_time, "end_time" => $end_time, "viet_performer" => $viet_performer,
                                "english_performer" => $english_performer, "viet_event" => $viet_event, "english_event" => $english_event, "day" => $day,
                                "stage" => $stage]]);

                $stmt->bind_result($image_link, $start_time, $end_time, $viet_performer, $english_performer, $viet_event, $english_event, $day, $stage);
            }

这基本上输出:

  

{"链接":" schedule_music.jpg"" START_TIME":" 17:00"" END_TIME&# 34;:" 18:30"" viet_performer":""" english_performer":"&#34 ;, " viet_event":""" english_event":"四环素   音乐""天":0,"级":1}   {"链接":" schedule_music.jpg"" START_TIME":" 11:00"" END_TIME&#34 ;: " 12:00"" viet_performer":""" english_performer":""&#34 ; viet_event":"新罕布什尔C"" english_event":"音乐""天":1,"阶段":0}

     

//再次重复

但是,根据JSON验证器,它是一个无效的JSON对象。

我想生成一个有效的JSON:

  

[{"链接":" schedule_music.jpg"" START_TIME":" 17:00"" END_TIME& #34;:" 18:30"" viet_performer":""" english_performer":"" " viet_event":""" english_event":"四环素   音乐""天":0,"级":1},{"链接":" schedule_music.jpg",& #34; START_TIME":" 11:00"" END_TIME":" 12:00"" viet_performer":&# 34;"" english_performer":""" viet_event":"新罕布什尔C"" english_event& #34;:"音乐""天":1,"级":0}]

注意开始/结束括号以及逗号分隔符。

我怎样才能做到这一点?

这是一个更好的例子(在某处找到)我的输出类似的样子:

{
    "id": "a1",
    "session": "General",
    "name": "Exhibitor Setup Begins",
    "startTime": "0900",
    "details": "9am Exhibitor Hall",
    "png": "image",
    "speaker1": "Johnson",
    "speaker2": "Nelson",
    "speaker3": ""
}{
    "id": "b1",
    "session": "General",
    "name": "Conference Registration",
    "startTime": "1000",
    "details": "10am Noon Upper Level Lobby",
    "png": "image",
    "speaker1": "Jackson",
    "speaker2": "",
    "speaker3": ""
}

我希望输出看起来像:

[
    {
        "id": "a1",
        "session": "General",
        "name": "Exhibitor Setup Begins",
        "startTime": "0900",
        "details": "9am Exhibitor Hall",
        "png": "image",
        "speaker1": "Johnson",
        "speaker2": "Nelson",
        "speaker3": ""
    },
    {
        "id": "b1",
        "session": "General",
        "name": "Conference Registration",
        "startTime": "1000",
        "details": "10am Noon Upper Level Lobby",
        "png": "image",
        "speaker1": "Jackson",
        "speaker2": "",
        "speaker3": ""
    }
]

2 个答案:

答案 0 :(得分:5)

关于更新的代码,问题在于您是单独编码每个部分而不是一次编码所有数据。试试这个:

$stmt->bind_result($image_link, $start_time, $end_time, $viet_performer, $english_performer, $viet_event,
    $english_event, $day, $stage);

$data = [];
while ($stmt->fetch()) {
    $data[] = [
        "image_link"        => $image_link,
        "start_time"        => $start_time,
        "end_time"          => $end_time,
        "viet_performer"    => $viet_performer,
        "english_performer" => $english_performer,
        "viet_event"        => $viet_event,
        "english_event"     => $english_event,
        "day"               => $day,
        "stage"             => $stage
    ];
}

echo json_encode($data);

基于早期版本问题的回复:

看起来你想要一个对象数组:

echo json_encode([
    [
        "image_link"        => $image_link,
        "start_time"        => $start_time,
        "end_time"          => $end_time,
        "viet_performer"    => $viet_performer,
        "english_performer" => $english_performer,
        "viet_event"        => $viet_event,
        "english_event"     => $english_event,
        "day"               => $day,
        "stage"             => $stage
    ]
]);

请注意,包含字符串键的数组将由json_encode转换为对象。在上面的代码中,内部数组变为对象,而外部数组则不是。

这可能是一种更直观的方式来看待它:

$objectOne = (object) [
    "image_link"        => $image_link,
    "start_time"        => $start_time,
    "end_time"          => $end_time,
    "viet_performer"    => $viet_performer,
    "english_performer" => $english_performer,
    "viet_event"        => $viet_event,
    "english_event"     => $english_event,
    "day"               => $day,
    "stage"             => $stage
];

$objectTwo = (object) [
    "image_link"        => $image_link,
    "start_time"        => $start_time,
    "end_time"          => $end_time,
    "viet_performer"    => $viet_performer,
    "english_performer" => $english_performer,
    "viet_event"        => $viet_event,
    "english_event"     => $english_event,
    "day"               => $day,
    "stage"             => $stage
];

echo json_encode([$objectOne, $objectTwo]);

答案 1 :(得分:0)

正确的方法是首先生成数据结构,然后对其进行编码:

$foo = [];
while($stmt->fetch()) {
    $foo []= [
         "image_link" => $image_link,
         "start_time" => $start_time,
         "end_time" => $end_time,
         "viet_performer" => $viet_performer,
         "english_performer" => $english_performer,
         "viet_event" => $viet_event,
         "english_event" => $english_event,
         "day" => $day,
         "stage" => $stage];
    ];
}
$json = json_encode($foo);