admin管理员组

文章数量:1426058

Please answer me as example Select code

  1. I want to add values into array local file JSON (insert member to JSON)
  2. element is also replaced my element like type, name... in JSON (update member in JSON)
  3. delete number line where id="1" and email="[email protected]" (delete memebr in JSON)

JSON file : report.json

var amir='{"reports":[' +
    '{"id": "1","type": "admin","name": "amir","email": "[email protected]","password": "123"},' +
    '{"id": "2","type": "member","name": "kevin","email": "[email protected]","password": "1234"}]}';

example select code for check register admin :

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");
    }
}

Please answer me as example Select code

  1. I want to add values into array local file JSON (insert member to JSON)
  2. element is also replaced my element like type, name... in JSON (update member in JSON)
  3. delete number line where id="1" and email="[email protected]" (delete memebr in JSON)

JSON file : report.json

var amir='{"reports":[' +
    '{"id": "1","type": "admin","name": "amir","email": "[email protected]","password": "123"},' +
    '{"id": "2","type": "member","name": "kevin","email": "[email protected]","password": "1234"}]}';

example select code for check register admin :

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");
    }
}
Share Improve this question edited Nov 11, 2015 at 13:57 Jay Blanchard 34.4k17 gold badges80 silver badges126 bronze badges asked Nov 11, 2015 at 13:51 fbi_amirfbi_amir 311 gold badge1 silver badge5 bronze badges 5
  • how do you include json file in your code? what's the include method? – murrometz Commented Nov 11, 2015 at 14:00
  • 1 Javascript in the browser does not have the privileges to edit files on disk. – Steve Commented Nov 11, 2015 at 14:00
  • That's right. I think you should write php file for manipulating you json. (Use json_decode, json_encode functions.) Than speak with it by your javascript (ajax-post, ajax-get etc) – murrometz Commented Nov 11, 2015 at 14:03
  • Or use a database. Anyway you have to have a php-interface for making changes. – murrometz Commented Nov 11, 2015 at 14:04
  • @user1011278 with syntax code in html : <script src="javascript/report.json"></script> – fbi_amir Commented Nov 11, 2015 at 14:11
Add a ment  | 

1 Answer 1

Reset to default 2

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 puter, depending on their browser settings. See http://www.w3schools./jsref/met_doc_open.asp.

See below for manipulation of JSON object.

var amir='{"reports":[' +
    '{"id": "1","type": "admin","name": "amir","email": "[email protected]","password": "123"},' +
    '{"id": "2","type": "member","name": "kevin","email": "[email protected]","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": "[email protected]",
  "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 == [email protected]
  if(obj.reports[c].id == "1" && obj.reports[c].email == "[email protected]") {
    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>

本文标签: jqueryHow to edit local JSON file with JavaScriptStack Overflow