Json instance details tutorial

Source: Internet
Author: User

Official Website: http://www.json.org/json-zh.html
Json2.js script home
Copy codeThe 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> <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 <persons. length; I ++) {var cur_person = persons [I]; str + = cur_person.name + "'sex is" + cur_person.sex + "and age is" + cur_person.age + "<br> ";}
Document. writeln (str );
// Convert to json text
Var myjsonobj = JSON. stringify (jsonObj2 );
Document. writeln (myjsonobj );
</Script>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.