2013-07-18 62 views
0

我正在嘗試使用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。我怎樣才能解決這個問題?

+0

展示的php代碼 – mplungjan

+0

爲什麼你的JSON文件與'開始}'是不是像你說的文件結尾所以必須有在頂部更多JSON – DevZer0

+0

我添加了php代碼並「優化」了json輸出。 ;-) – Martin

回答

0

正如我在我的評論說

$json = file_get_contents($file); 

// $json is now a string 

$data = json_decode($json); 

// $data is a PHP object 
// So lets call the second array $data->someArray 
// since I do not know what it is called looking at your file 

// convert form data to PHP array 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.. 

// $postArray is a PHP object 

// $json = json_encode($postArray); // do NOT convert here 

array_push($data->someArray, $postArray); 
$json = json_encode($data); 
// write to file 
file_put_contents($file, $json, FILE_APPEND); 
+0

我稍後使用$數據來顯示現有項目。在這部分我不會使用它。 – Martin

+0

所以重命名.... – mplungjan

+0

謝謝,這是答案。一個關鍵的問題。如何在追加新數據後更好地格式化json文件?有換行符和製表符? – Martin

0

你的值字段是一個對象而不是對象的數組(php關聯數組的編碼是一個json對象)。因此,對於具有方括號而不是「values」=> array()的值,您需要「values」=> array(array(「id」=> ...等等))

至於你的第一個問題你已經顛倒了json_encoding。首先將你的postArray推入數據,然後json_encode數據。

+0

感謝您的第一個提示。有用。對不起,這個問題,但你的意思是「推動你的postArray到數據」? – Martin

+0

你想添加postArray到數據,以便json文件包含新創建的數據,對嗎?你的$ data是一個數組,所以你可以做array_push($ data,$ postArray)。並確保在最後的json_encode操作之前執行此操作。 –