Javascript 如何使用jQuery添加/附加到外部JSON文件

Javascript 如何使用jQuery添加/附加到外部JSON文件,javascript,jquery,json,Javascript,Jquery,Json,我有一个json文件,我想构建一个表单,允许我在文件中添加/编辑元素。是否有jQuery函数/方法允许我在外部json文件中发布和附加元素 不确定这是否有帮助,但当前的json结构如下所示: [{“cast”:“, “董事”:“董事”, “体裁”:“, “id”:假, “投票数”:0, “绘图”:“, “评级”:0, “运行时”:0, “名称”:“名称”, “年”:假 }, {“演员阵容”:“蒂姆·罗宾斯、摩根·弗里曼、鲍勃·冈顿,”, “董事”:“Frank Darabont”, “类型”:“

我有一个json文件,我想构建一个表单,允许我在文件中添加/编辑元素。是否有jQuery函数/方法允许我在外部json文件中发布和附加元素

不确定这是否有帮助,但当前的json结构如下所示:

[{“cast”:“,
“董事”:“董事”,
“体裁”:“,
“id”:假,
“投票数”:0,
“绘图”:“,
“评级”:0,
“运行时”:0,
“名称”:“名称”,
“年”:假
},
{“演员阵容”:“蒂姆·罗宾斯、摩根·弗里曼、鲍勃·冈顿,”,
“董事”:“Frank Darabont”,
“类型”:“犯罪剧”,
“id”:“011161”,
“投票数”:968510,
“阴谋”:“两名被监禁的男子在数年的时间里结合在一起,通过共同的体面行为找到安慰和最终的救赎。”,
“评级”:93000000000007,
“运行时”:142,
“标题”:“肖申克的救赎”,
“年份”:“1994年”
}]
非常感谢你!!(:如果您使用的是jQuery的或,那么您可以操作一个javascript对象。例如:

$.getJSON( "/test.json", function( data ) {
  // now data is JSON converted to an object / array for you to use.
  alert( data[1].cast ) // Tim Robbins, Morgan Freeman, Bob Gunton

  var newMovie = {cast:'Jack Nicholson', director:...} // a new movie object

  // add a new movie to the set
  data.push(newMovie);      
});
您现在所要做的就是保存文件。您可以使用jQuery.post()将文件发送回服务器以保存文件

更新:发布示例

在您的服务器上,使用PHP的示例

$updatedData = $_POST['newData'];
// please validate the data you are expecting for security
file_put_contents('path/to/thefile.json', $updatedData);
//return the url to the saved file

您目前是如何获得JSON文件的?@akshaynagpal,您将以
数据
的形式发布更新后的JSON,而不是以
文件
的形式发布。我将更新answermyServerUrl,只需包含服务器url或/update.php?确切的url。显然,如果它包含
update.php
,那么您必须包含它。请询问新任务ion@akshaynagpal已经有问题了-
$updatedData = $_POST['newData'];
// please validate the data you are expecting for security
file_put_contents('path/to/thefile.json', $updatedData);
//return the url to the saved file
<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>
{  
  "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"
  }
   ]
}