dsl2014
2013-07-18 08:16
浏览 93
已采纳

编辑Json文件

I am trying to use a jQuery gantt as a wordpress plugin. Currently I'm stuck on editing the data.json. I use a php form to populate a new item. When submitting the form, it will add data to the file, but behind the closing square brackets.

[{
  ...

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

After submitting the form it looks like this:

[{
  ...

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

This is the requested php Code which will adding stuff to the 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);

I also can't establish the square bracket after value. How can I fix this?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanlei20082008 2013-07-18 09:11
    已采纳

    As I said in my comment

    $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);
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题