0

我正在尝试使用 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. 我怎样才能解决这个问题?

4

2 回答 2

0

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

至于您的第一个问题,您已经反转了 json_encoding。首先将您的 postArray 推入数据,然后 json_encode 数据。

于 2013-07-18T08:56:34.927 回答
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);
于 2013-07-18T09:11:33.833 回答