0
因此,我使用PHP腳本將JSON輸出到MySQL,但我很難弄清楚如何輸出正確的JSON格式。MySQL到JSON,如何獲得正確的格式?
這裏的PHP腳本:
$sql_query = "SELECT * FROM DiseaseData";
$res_sql = mysql_query($sql_query) or die(mysql_error());
$arr = array();
if(mysql_num_rows($res_sql) > 0){
ini_set('memory_limit', '-1');
while($row_sql = mysql_fetch_assoc($res_sql)){
$arr[] = $row_sql;
}
$json = json_encode($arr);
$file = '../../files/json/DiseaseData.json';
file_put_contents($file, $json);
}
ini_set('memory_limit', '-1');
這裏的輸出JSON格式:
[{
"ID": "1",
"Magnitude": "0.842",
"County": "Alameda",
"Disease": "E. coli O157",
"lat": "37.7652",
"lng": "-122.242"
}, {
"ID": "2",
"Magnitude": "1.520",
"County": "Alameda",
"Disease": "HIV",
"lat": "37.7652",
"lng": "-122.242"
}]
這是我想擁有它的JSON格式:
{
"columns":[{
"fieldName" : "ID",
"position" : 1
},
{
"fieldName" : "Magnitude",
"position" : 2
},
{
"fieldName" : "County",
"position" : 3
},
{
"fieldName" : "Disease",
"position" : 4
},
{
"fieldName" : "lat",
"position" : 5
},
{
"fieldName" : "lng",
"position" : 6
},]
"data": [
[ 1, 0.842, "Alameda", "E. coli O157", 37.7652, -122.242],
[ 2, 1.520, "Alameda", "HIV", 37.7652, -122.242]
]
}
謝謝,似乎像魅力一樣工作! 唯一的問題是,在輸出的JSON和列切換位置,列「ID」返回布爾值「false」。 ' 「列」:[{ \t \t 「fieldName的」: 「幅度」, \t \t 「位置」:1 \t},{ \t \t 「fieldName的」: 「縣」, \t \t 「位置」 :2 \t},{ \t \t 「fieldName的」: 「疾病」, \t \t 「位置」:3 \t},{ \t \t 「fieldName的」: 「LAT」, \t \t 「位置」:4 \t},{ \t \t 「fieldName的」: 「LNG」, \t \t 「位置」:5 \t},{ \t \t「 fieldName「:false, \t \t」position「:6 \t}]' –
@AdamDedanga哦,那是因爲'for'循環,它應該是'for($ i = 0; $ i <$ max_columns; $ i ++){...',而不是''position'=> $ i',它應該是''position'=> $ i + 1'。我已經更新了我的答案。 –