编辑Json文件

时间:2013-07-18 08:16:53

标签: php jquery json gantt-chart

我正在尝试使用jQuery甘特作为wordpress插件。目前我一直在编辑data.json。我使用php表单填充新项目。提交表单时,它会将数据添加到文件中,但在结束方括号后面。

[{
  ...

 },
 {  "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}}

这是请求的PHP代码,它将向json添加内容:

$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之后建立方括号。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

您的值字段是一个对象数组而不是一个对象(php关联数组的编码是一个json对象)。因此,对于具有方括号而不是“值”的值=> array()你需要“values”=> array(array(“id”=> ... etc.))

至于你的第一个问题,你已经颠倒了json_encoding。首先将postArray推入数据,然后推送json_encode数据。

答案 1 :(得分: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);