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 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示