我正在尝试从 MySQL 表中获取数据并以以下格式创建 JSON 输出。
{
markers: [
{
latitude: 57.7973333,
longitude: 12.0502107,
title: "Angered",
content: "Representing :)"
},
{
latitude: 57.6969943,
longitude: 11.9865,
title: "Gothenburg",
content: "Swedens second largest city"
}
]
}
这是我用来生成 JSON 的 PHP
$model = array();
$query = "SELECT title, content, lat, lng FROM locations_tbl";
//Get records from database
$result = mysql_query($query, $con);
if(mysql_num_rows($result)) {
while($e = mysql_fetch_assoc($result)) {
$model['title'][] = $e['title'];
$model['content'][] = $e['content'];
$model['lat'][] = $e['lat'];
$model['lng'][] = $e['lng'];
}
}
header('Content-type: application/json');
print json_encode(array('marker'=>$model));
下面是我用上面的代码得到的输出:
{
marker: {
title: [
"Marker 1",
"Marker 2",
"Marker 3",
"Marker 4",
"Marker 5"
],
content: [
"Text 1",
"Text 2",
"Text 3",
"Text 4",
"Text 5"
],
lat: [
"46.99065400",
"47.03520400",
"47.20387700",
"47.62574900",
"47.43443400"
],
lng: [
"-122.92164800",
"-122.81614600",
"-122.24486400",
"-122.14453800",
"-122.46088200"
]
}
}
有什么建议么?