javascript - 如何使用 JavaScript 编辑本地 JSON 文件

标签 javascript jquery json

请回答我,例如Select代码

  1. 我想将值添加到数组本地文件 JSON(将成员插入 JSON)
  2. 元素也替换了我的元素,如 type , name ... JSON 格式(更新 JSON 格式的成员)
  3. 删除 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/

相关文章:

javascript - prop 的默认类型取决于另一个 prop 的值

javascript - 可定制的 jquery 警报框的问题

mysql - 谷歌地图标记 : directly from database, xml 或 json

javascript - 正则表达式将字符串与开头的(逗号分隔的)数字匹配并拆分为数字和其余部分?

javascript - Angular 服务在构造函数中调用子方法

javascript - 未捕获的类型错误 : undefined is not a function when using JSON object with name

java - 在javascript中将JSONArray转换为String以发送到java

jquery - 仪表板基础上有 5 个可排序的响应式网格

javascript - 使用 jquery-ui 在 droppable 中居中可拖动元素

php - 在 Zend 框架 2 中显示为来自 JSON 的网格