2015-12-18 145 views
1

我有PHP代碼,它從mysql數據庫的表中獲取數據並以JSON格式顯示該數據。如何更改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":[{"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代碼以獲得上述輸出?

回答

2

只是把它包起來:

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

非常感謝您的回答取代你的代碼的最後一行。有效。 – user5679217

+0

@ user5679217不客氣! ':)' –

1

嘗試用:

header('Content-type: application/json'); 
$result['records'] = $arRows; 
echo json_encode($result); 
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); 
0

只是

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