2

ファイル内の要素を追加/編集できるフォームを作成したいjsonファイルがあります。外部jsonファイルに要素を投稿および追加できるようにするjQuery関数/メソッドはありますか?

これが役立つかどうかはわかりませんが、現在の json 構造は次のとおりです。

 [ { "cast" : "",
    "director" : "",
    "genre" : "",
    "id" : false,
    "nrVotes" : 0,
    "plot" : "",
    "rating" : 0,
    "runtime" : 0,
    "title" : "",
    "year" : false
  },
  { "cast" : "Tim Robbins, Morgan Freeman, Bob Gunton, ",
    "director" : "Frank Darabont",
    "genre" : "Crime Drama ",
    "id" : "0111161",
    "nrVotes" : 968510,
    "plot" : "Two imprisoned men bond over a number of years, finding solace and eventual redemption through acts of common decency.",
    "rating" : 9.3000000000000007,
    "runtime" : 142,
    "title" : "The Shawshank Redemption",
    "year" : "1994"
  }]

よろしくお願いします!(:

4

2 に答える 2

0
<html>
<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.3.min.js" ></script>
</head>
<body>
    <?php
        $str = file_get_contents('data.json');//get contents of your json file and store it in a string
        $arr = json_decode($str, true);//decode it
         $arrne['name'] = "sadaadad";
         $arrne['password'] = "sadaadad";
         $arrne['nickname'] = "sadaadad";
         array_push( $arr['employees'], $arrne);//push contents to ur decoded array i.e $arr
         $str = json_encode($arr);
        //now send evrything to ur data.json file using folowing code
         if (json_decode($str) != null)
           {
             $file = fopen('data.json','w');
             fwrite($file, $str);
             fclose($file);
           }
           else
           {
             //  invalid JSON, handle the error 
           }

        ?>
    <form method=>
</body>

data.json

{  
  "employees":[  
  {  
     "email":"11BD1A05G9",
     "password":"INTRODUCTION TO ANALYTICS",
     "nickname":4
  },
  {  
     "email":"Betty",
     "password":"Layers",
     "nickname":4
  },
  {  
     "email":"Carl",
     "password":"Louis",
     "nickname":4
  },
  {  
     "name":"sadaadad",
     "password":"sadaadad",
     "nickname":"sadaadad"
  },
  {  
     "name":"sadaadad",
     "password":"sadaadad",
     "nickname":"sadaadad"
  },
  {  
     "name":"sadaadad",
     "password":"sadaadad",
     "nickname":"sadaadad"
  }
   ]
}
于 2016-08-23T02:05:29.537 に答える