如何更改JSON输出的格式?

时间:2015-12-18 18:00:34

标签: php mysql json

我有PHP代码,它从mysql数据库中的表中获取数据,并以JSON格式显示该数据。

这是我的代码:

<?php 

include ("config/config.php");
//build query
$query = 
"SELECT 
ProductID, 
Name, 
Price, 
Type,
cat 
FROM store ORDER BY ProductID ASC";

$rsPackages = mysqli_query($mysqli_conn,$query);

$arRows = array();

while ($row_rsPackages = mysqli_fetch_assoc($rsPackages)) {
   array_push($arRows, $row_rsPackages);
}

header('Content-type: application/json');
echo json_encode($arRows);

?>

此代码的输出如下所示:

 [{"ProductID":"1","Name":"HTML:Beginners guide","Price":"7.85","Type":"Book","cat":"HTML"},{"ProductID":"4","Name":"HTML: Intermediate","Price":"8.99","Type":"Book","cat":"HTML"},{"ProductID":"5","Name":"HTML: Advanced","Price":"10.99","Type":"Book","cat":"HTML"},{"ProductID":"7","Name":"CSS: Beginners Guide","Price":"7.99","Type":"Book","cat":"CSS"},{"ProductID":"8","Name":"CSS: Intermediate","Price":"8.99","Type":"Book","cat":"CSS"},{"ProductID":"9","Name":"CSS: Advanced","Price":"10.99","Type":"Book","cat":"CSS"},{"ProductID":"10","Name":"PHP: Beginners Guide","Price":"7.99","Type":"Book","cat":"PHP"},{"ProductID":"11","Name":"PHP: Intermediate","Price":"8.99","Type":"Book","cat":"PHP"},{"ProductID":"12","Name":"PHP: Advanced","Price":"10.99","Type":"Book","cat":"PHP"},{"ProductID":"13","Name":"MYSQL- Easy steps","Price":"11.99","Type":"Book","cat":"MYSQL"},{"ProductID":"14","Name":"HTML- Video Guide","Price":"19.99","Type":"CD","cat":"HTML"},{"ProductID":"15","Name":"CSS: Video Guide","Price":"19.99","Type":"CD","cat":"CSS"},{"ProductID":"16","Name":"PHP: Video Guide","Price":"19.99","Type":"CD","cat":"PHP"},{"ProductID":"22","Name":"css book","Price":"3.49","Type":"","cat":""},{"ProductID":"26","Name":"bdkjhedsjbdsasa","Price":"3.59","Type":"","cat":""}]

但是,我希望输出结果以这样的格式显示:“{”records“:[results]}”

示例:

{"records":[{"ProductID":"1","Name":"HTML:Beginners guide","Price":"7.85","Type":"Book","cat":"HTML"},{"ProductID":"4","Name":"HTML: Intermediate","Price":"8.99","Type":"Book","cat":"HTML"},{"ProductID":"5","Name":"HTML: Advanced","Price":"10.99","Type":"Book","cat":"HTML"},{"ProductID":"7","Name":"CSS: Beginners Guide","Price":"7.99","Type":"Book","cat":"CSS"},{"ProductID":"8","Name":"CSS: Intermediate","Price":"8.99","Type":"Book","cat":"CSS"},{"ProductID":"9","Name":"CSS: Advanced","Price":"10.99","Type":"Book","cat":"CSS"},{"ProductID":"10","Name":"PHP: Beginners Guide","Price":"7.99","Type":"Book","cat":"PHP"},{"ProductID":"11","Name":"PHP: Intermediate","Price":"8.99","Type":"Book","cat":"PHP"},{"ProductID":"12","Name":"PHP: Advanced","Price":"10.99","Type":"Book","cat":"PHP"},{"ProductID":"13","Name":"MYSQL- Easy steps","Price":"11.99","Type":"Book","cat":"MYSQL"},{"ProductID":"14","Name":"HTML- Video Guide","Price":"19.99","Type":"CD","cat":"HTML"},{"ProductID":"15","Name":"CSS: Video Guide","Price":"19.99","Type":"CD","cat":"CSS"},{"ProductID":"16","Name":"PHP: Video Guide","Price":"19.99","Type":"CD","cat":"PHP"},{"ProductID":"22","Name":"css book","Price":"3.49","Type":"","cat":""},{"ProductID":"26","Name":"bdkjhedsjbdsasa","Price":"3.59","Type":"","cat":""}]}

我应该对我的php代码进行哪些更改才能获得上述输出?

4 个答案:

答案 0 :(得分:2)

把它包起来:

echo json_encode(array("records" => $arRows));

答案 1 :(得分:1)

尝试:

header('Content-type: application/json');
$result['records'] = $arRows;
echo json_encode($result);

答案 2 :(得分:1)

你需要解析返回变量的内容并将其添加到新的json对象中,就像我现在要展示的那样。

var data = JSON.parse('[{"ProductID":"1","Name":"HTML:Beginners guide","Price":"7.85","Type":"Book","cat":"HTML"},{"ProductID":"4","Name":"HTML: Intermediate","Price":"8.99","Type":"Book","cat":"HTML"},{"ProductID":"5","Name":"HTML: Advanced","Price":"10.99","Type":"Book","cat":"HTML"},{"ProductID":"7","Name":"CSS: Beginners Guide","Price":"7.99","Type":"Book","cat":"CSS"},{"ProductID":"8","Name":"CSS: Intermediate","Price":"8.99","Type":"Book","cat":"CSS"},{"ProductID":"9","Name":"CSS: Advanced","Price":"10.99","Type":"Book","cat":"CSS"},{"ProductID":"10","Name":"PHP: Beginners Guide","Price":"7.99","Type":"Book","cat":"PHP"},{"ProductID":"11","Name":"PHP: Intermediate","Price":"8.99","Type":"Book","cat":"PHP"},{"ProductID":"12","Name":"PHP: Advanced","Price":"10.99","Type":"Book","cat":"PHP"},{"ProductID":"13","Name":"MYSQL- Easy steps","Price":"11.99","Type":"Book","cat":"MYSQL"},{"ProductID":"14","Name":"HTML- Video Guide","Price":"19.99","Type":"CD","cat":"HTML"},{"ProductID":"15","Name":"CSS: Video Guide","Price":"19.99","Type":"CD","cat":"CSS"},{"ProductID":"16","Name":"PHP: Video Guide","Price":"19.99","Type":"CD","cat":"PHP"},{"ProductID":"22","Name":"css book","Price":"3.49","Type":"","cat":""},{"ProductID":"26","Name":"bdkjhedsjbdsasa","Price":"3.59","Type":"","cat":""}]');
var result = new Object();
result.result = data;
console.log(result);

答案 3 :(得分:0)

只需用

替换代码的最后一行
$data = array("records" => $arRows);
echo json_encode($data);