请回答我,例如Select
代码
- 我想将值添加到数组本地文件 JSON(将成员插入 JSON)
- 元素也替换了我的元素,如
type
,name
... JSON 格式(更新 JSON 格式的成员) - 删除
id="1"
处的数轴和email="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d1b0bcb8a391a2b8a5b4ffb2bebc" rel="noreferrer noopener nofollow">[email protected]</a>"
(删除JSON中的memebr)
JSON 文件:report.json
var amir='{"reports":[' +
'{"id": "1","type": "admin","name": "amir","email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5e3f33372c1e2d372a3b703d3133" rel="noreferrer noopener nofollow">[email protected]</a>","password": "123"},' +
'{"id": "2","type": "member","name": "kevin","email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d9b8bd99b8bdf7bab6b4" rel="noreferrer noopener nofollow">[email protected]</a>","password": "1234"}]}';
示例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");
}
}
最佳答案
至于将操作的 JSON 结果保存到磁盘,这必须在后端完成,或者您可以打开一个以文件为内容的窗口,将 MIME 类型设置为 json,这可能会提示用户将其保存到他们的计算机上,具体取决于他们的浏览器设置。请参阅http://www.w3schools.com/jsref/met_doc_open.asp .
请参阅下文了解 JSON 对象的操作。
var amir='{"reports":[' +
'{"id": "1","type": "admin","name": "amir","email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1b7a7672695b68726f7e35787476" rel="noreferrer noopener nofollow">[email protected]</a>","password": "123"},' +
'{"id": "2","type": "member","name": "kevin","email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2445406445400a474b49" rel="noreferrer noopener nofollow">[email protected]</a>","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": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="630210070523040e020a0f4d000c0e" rel="noreferrer noopener nofollow">[email protected]</a>",
"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 == <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f495999d86b4879d8091da979b99" rel="noreferrer noopener nofollow">[email protected]</a>
if(obj.reports[c].id == "1" && obj.reports[c].email == "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="70111d190230031904155e131f1d" rel="noreferrer noopener nofollow">[email protected]</a>") {
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>
关于javascript - 如何使用 JavaScript 编辑本地 JSON 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33651997/