我正在尝试让tag-it jquery插件与json字符串一起工作。目前,我从数据库中获取值的方式如下:
$query = sprintf(
'SELECT
t.tag
FROM
tags AS t
');
$row_set= array();
if($result = mysqli_query($db, $query))
{
// fetch data
while ($row = mysqli_fetch_assoc($result))
{
$row_set[] = $row;
}
// set the output
echo json_encode($row_set);
}在AJAX中调用时,它会给出以下输出:
[{"tag":"test"},{"tag":"tests"}]但我必须输出以下格式的JSON字符串:
["android-intent","animate","architecture","artificial-intelligence","attributes"]我如何才能做到这一点?
发布于 2012-04-28 19:22:18
当你使用row_set[] = $row;时,$row很可能看起来像这样的$row['tag'] = 'test,这就是为什么你使用JSON对象格式;
试一试
$row_set[] = $row['tag']; 确保还将JSON参数设置为true,以使seound始终返回数组
json_encode($row_set,true);这将以数组的形式返回it,而不是对象
发布于 2012-04-28 19:23:58
这应该可以解决这个问题,将字符串添加到箭头中,而不是将数组添加到数组中。
$row_set[] = $row[0]; or $row_set[] = $row['tag'];https://stackoverflow.com/questions/10363003
复制相似问题