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

编辑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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?