现在我的代码:
$sql = "SELECT * FROM schedule_feednow WHERE IDMac = '".$_GET["IDMac"]."'";
$result = mysql_query($sql) or die(mysql_error());
$json_data = array();
while($rec = mysql_fetch_assoc($result)){
$Subjson = array();
$Subjson['IDMac'] = $rec['IDMac'];
$Subjson['DATETIME'] = $rec['DATETIME'];
$Subjson['Weight'] = $rec['Weight'];
array_push($json_data,$Subjson);
}
echo json_encode ($json_data);
和结果:
[{"IDMac":"C-01","DATETIME":"12:05:19 AM on June 23, 2017","Weight":"50"},{"IDMac":"C-01","DATETIME":"12:05:55 AM on June 23, 2017","Weight":"50"},{"IDMac":"C-01","DATETIME":"02:02:20 PM on June 23, 2017","Weight":"50"}]
但我不想要这个结果
我想要这个结果 - >
["Schedule":{"IDMac":"C-01","DATETIME":"12:05:19 AM on June 23, 2017","Weight":"50"},{"IDMac":"C-01","DATETIME":"12:05:55 AM on June 23, 2017","Weight":"50"},{"IDMac":"C-01","DATETIME":"02:02:20 PM on June 23, 2017","Weight":"50"}]
请帮我生成代码或训练我。 非常感谢你。
答案 0 :(得分:2)
只需将当前结果放入带有“Schedule”键的数组中:
echo json_encode(array('Schedule' => $json_data));
答案 1 :(得分:1)
只需创建另一个数组并将您的json数据放入:
$newJsonData = ['Schedule' => $json_data];
echo json_encode ($newJsonData);
答案 2 :(得分:1)
替换
echo json_encode ($json_data);
与
$myResult = ['Schedule' => $json_data];
echo json_encode ($myResult);
答案 3 :(得分:0)
$sql = "SELECT * FROM schedule_feednow WHERE IDMac = '".$_GET["IDMac"]."'";
$result = mysql_query($sql) or die(mysql_error());
$json_data = array();
while($rec = mysql_fetch_array($result)){
$json_data[] = $rec;
}
echo json_encode ('Schedule' => $json_data);