方括号外缺少JSON


JSON missing outside square brackets

我需要在数组中有JSON对象,如:

[{"term":"hemisected","description":"Cut into two equal parts; to bisect, especially along a medial longitudinal plane."},{"term":"polyuria","description":"A condition usually defined as excessive or abnormally large production or passage of urine."},{"term":"dyspnoea","description":"Shortness of breath."}]

然而,以下是作为单独的对象输出JSON:

while ($row = mysql_fetch_array($result)) { 
        $data = array(
        'term' => $row['term'],
        'description' => $row['definition']
        );
echo json_encode($data);
}

:

{"term":"hemisected","description":"Cut into two equal parts; to bisect, especially along a medial longitudinal plane."}{"term":"polyuria","description":"A condition usually defined as excessive or abnormally large production or passage of urine."}{"term":"dyspnoea","description":"Shortness of breath."} 

目前,json结构没有很好地构建,因为它在循环期间被调用。它必须在构建数组(在本例中为$data)之后调用。考虑这个例子:

$data = array();
while ($row = mysql_fetch_array($result)) { 
    $data[] = array(
        'term' => $row['term'],
        'description' => $row['definition'],
    );
}
echo json_encode($data);