How to Use json2

Source: Internet
Author: User
<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. * "}]}; // declaration string. You can compare the differences between JSON text and normal text. var normalstring = '[{persons: [{name:" Jordan ", S Ex: "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>'); // converts a JSON object to a string var text = JSON. stringify (Mye); // compares 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 the 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 operations are JSON objects. You can view the operation results by removing comments // Jsonobj2.persons. push (person); // Add a record at the end of the array // jsonobj2.persons. pop (); // Delete the last entry // jsonobj2.persons. shift (); // Delete the first 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 // replace 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, number of deletions, 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>

Loading editor...

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.