[英]How to edit local JSON file with JavaScript
Please answer me as example Select
code请回答我作为例子
Select
代码
type
, name
... in JSON (update member in JSON) type
, name
... in JSON (update member in JSON)id="1"
and email="amir@site.com"
(delete memebr in JSON)id="1"
和email="amir@site.com"
数字行(删除 JSON 中的 memebr) JSON file : report.json
JSON 文件:
report.json
var amir='{"reports":[' +
'{"id": "1","type": "admin","name": "amir","email": "amir@site.com","password": "123"},' +
'{"id": "2","type": "member","name": "kevin","email": "ad@ad.com","password": "1234"}]}';
example select
code for check register admin :用于检查注册管理员的示例
select
代码:
var obj = JSON.parse(amir);
for(c = 0; c <= obj.reports.length - 1; c++) {
type = obj.reports[c].type.toString();
if (type == "admin") {
active = 1;
}
if (c == obj.reports.length - 1) {
if (active == 1)
alert("Yes");
else
alert("No");
}
}
As far as saving the result of manipulated JSON to the disk, that has to be done on the back end, or you can open a window with the file as content, setting the MIME type to json, which could prompt the user to save it to their computer, depending on their browser settings.至于将操作JSON的结果保存到磁盘,必须在后端完成,或者您可以打开一个以文件为内容的窗口,将MIME类型设置为json,这会提示用户保存它到他们的计算机,具体取决于他们的浏览器设置。 See http://www.w3schools.com/jsref/met_doc_open.asp .
请参阅http://www.w3schools.com/jsref/met_doc_open.asp 。
See below for manipulation of JSON object.有关 JSON 对象的操作,请参见下文。
var amir='{"reports":[' + '{"id": "1","type": "admin","name": "amir","email": "amir@site.com","password": "123"},' + '{"id": "2","type": "member","name": "kevin","email": "ad@ad.com","password": "1234"}]}'; var obj = JSON.parse(amir); document.getElementById("before").innerHTML = JSON.stringify(obj); console.log("Before", JSON.parse(JSON.stringify(obj))); // objects use pointers, clone it to see the value at this point // Add a new member into the array (example, using made up values) obj.reports.push({ "id": ""+obj.reports.length + 1, "type": "member", "name": "Joe", "email": "asdf@gmail.com", "password": "ajdj12oi42" }); document.getElementById("during").innerHTML = JSON.stringify(obj); console.log("During", JSON.parse(JSON.stringify(obj))); // objects use pointers, clone it to see the value at this point // When deleting items, it is often easier to start high, and end low for(var c = obj.reports.length - 1; c >= 0; c--) { // Delete member in JSON where id == 1 and email == amir@site.com if(obj.reports[c].id == "1" && obj.reports[c].email == "amir@site.com") { obj.reports.splice(c, 1); } else { // Add values into the objects (example, using random numbers) obj.reports[c].newKey = "New Value! " + Math.floor(Math.random() * 100); } } document.getElementById("after").innerHTML = JSON.stringify(obj); console.log("After", JSON.parse(JSON.stringify(obj))); // objects use pointers, clone it to see the value at this point
<h1>Before</h1> <div id="before"></div> <h1>During</h1> <div id="during"></div> <h1>After</h1> <div id="after"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.