我正在嘗試使用jQuery甘特圖作爲wordpress插件。目前,我一直在編輯data.json。我使用一個PHP表單來填充一個新項目。提交表單時,它會將數據添加到文件中,但在方括號後面。編輯Json文件
[{
...
},
{ "name" : "Vermessung"
, "desc" : ""
, "values": [
{ "id" : "5"
, "from" : "/Date(1363132800000)/"
, "to" : "/Date(1368655200000)/"
, "desc" : "Vom Beauftragen der Vermessung bis zur tatsächlichen Vermessung"
, "customClass": "ganttBlue"
, "label" : "Vermessung"
}
]
}
]
提交表格後,它看起來是這樣的:
[{
...
},
{ "name" : "Vermessung"
, "desc" : ""
, "values": [
{ "id" : "5"
, "from" : "/Date(1363132800000)/"
, "to" : "/Date(1368655200000)/"
, "desc" : "Vom Beauftragen der Vermessung bis zur tatsächlichen Vermessung"
, "customClass": "ganttBlue"
, "label" : "Vermessung"
}
]
}
]{"name":null,"desc":null,"values":{"id":null,"from":null,"to":null,"desc":null,"customClass":null,"label":null}}
這將增加東西向JSON請求的PHP代碼:
$file = jQg_BASENAME_DIR.'/inc/data.json';
log_me('This is a message for debugging purposes');
if(isset($_POST['submit'])){
$json = file_get_contents($file);
$data = json_decode($json);
// convert form data to json format
$postArray = array(
"name" => $_POST['name'],
"desc" => $_POST['desc'],
"values" => array(
"id" => $_POST["value_id"],
"from" => $_POST['value_from'],
"to" => $_POST['value_to'],
"desc" => $_POST['value_desc'],
"customClass" => $_POST['value_class'],
"label" => $_POST['value_label']
)
); //you might need to process any other post fields you have..
$json = json_encode($postArray);
array_push($json, $postArray);
// write to file
file_put_contents($file, $json, FILE_APPEND);
我也不能建立方括號後爲value
。我怎樣才能解決這個問題?
展示的php代碼 – mplungjan
爲什麼你的JSON文件與'開始}'是不是像你說的文件結尾所以必須有在頂部更多JSON – DevZer0
我添加了php代碼並「優化」了json輸出。 ;-) – Martin