2

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");
    }
}
5
  • how do you include json file in your code? what's the include method? Commented Nov 11, 2015 at 14:00
  • 1
    Javascript in the browser does not have the privileges to edit files on disk. 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) Commented Nov 11, 2015 at 14:03
  • Or use a database. Anyway you have to have a php-interface for making changes. Commented Nov 11, 2015 at 14:04
  • @user1011278 with syntax code in html : <script src="javascript/report.json"></script> Commented Nov 11, 2015 at 14:11

1 Answer 1

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 computer, depending on their browser settings. See http://www.w3schools.com/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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.