我需要在数组中包含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."} 发布于 2014-06-17 03:30:14
目前,json结构没有很好地构建,因为它在循环期间被调用。必须在构建数组(在本例中为$data)之后调用它。考虑一下这个例子:
$data = array();
while ($row = mysql_fetch_array($result)) {
$data[] = array(
'term' => $row['term'],
'description' => $row['definition'],
);
}
echo json_encode($data);https://stackoverflow.com/questions/24255222
复制相似问题