CopyCode The Code is as follows: <SCRIPT type = "text/JavaScript" src = "json2.js"> </SCRIPT>
<SCRIPT>
// Directly declare the JSON Data Structure
VaR myjsonobject = {"bindings ":[
{"Ircevent": "PRIVMSG", "method": "newuri", "RegEx": "^ http ://.*"},
{"Ircevent": "PRIVMSG", "method": "deleteuri", "RegEx": "^ Delete .*"},
{"Ircevent": "PRIVMSG", "method": "randomuri", "RegEx": "^ random .*"}
]
};
// Declare the string to compare the difference between the JSON text and our normal text
VaR normalstring = '[{persons: [{name: "Jordan", sex: "M", age: "40" },{ name: "Bryant", sex: "M", age: "28" },{ name: "McGrady", sex: "M", age: "27"}] ';
VaR jsontext = '[{"persons": [{"name": "Jordan", "sex": "M", "Age": "40 "}, {"name": "Bryant", "sex": "M", "Age": "28" },{ "name": "McGrady", "sex ": "M", "Age": "27"}] ';
// Call the eval function to convert it to a JSON object,
VaR Mye = eval (normalstring );
Document. writeln (Mye '<br> ');
// Convert a JSON object to a string
VaR text = JSON. stringify (Mye );
// Compare the converted JSON text with the declared text
Document. writeln ('converted JSON text: 'text' <br> declared JSON text 'jsontext' <br> declared plain text 'normalstring' <br> ');
// JSON resolution is better when security is important. JSON parsing only recognizes JSON text and is more secure. The following calls the JSON parse function to convert text data to generate a JSON data structure.
VaR mydata = JSON. parse (jsontext );
Document. writeln (mydata' <br> ');
// The following operations add, query, modify, and delete a JSON object
// Declare a JSON object
VaR jsonobj2 = {persons: [{name: "Jordan", sex: "M", age: "40" },{ name: "Bryant", sex: "M ", age: "28" },{ name: "McGrady", sex: "M", age: "27"}]};
VaR persons = jsonobj2.persons;
VaR STR = "";
VaR person = {name: "Yaoming", sex: "M", age: "26 "};
// The following is the JSON object operation. You can view the operation result by removing the annotation.
// Jsonobj2.persons. Push (person); // Add a record to the array
// Jsonobj2.persons. Pop (); // Delete the last entry
// Jsonobj2.persons. Shift (); // Delete the first entry
Jsonobj2.persons. unshift (person); // Add a record at the beginning of the array. Any method suitable for javascript can be used in the array of JSON objects! So there is another method splice () for crud operations! // Delete
// Jsonobj2.persons. splice (); // start position, delete count
// Do not delete the replacement
VaR self = {name: "Tom", sex: "M", age: "24 "};
VaR brother = {name: "Mike", sex: "M", age: "29 "};
Jsonobj2.persons. splice (, self, brother, self); // start position, delete count, insert object
// Replace and delete
// Jsonobj2.persons. splice (0, 1, self, Brother); // start position, delete count, insert object
for (VAR I = 0; I ";}
document. writeln (STR);
// convert to JSON text
var myjsonobj = JSON. stringify (jsonobj2);
document. writeln (myjsonobj);