JavaScript Json2 using methods _javascript Tips

Source: Internet
Author: User
Tags data structures
Copy Code code as follows:

<script type= "Text/javascript" src= "Json2.js" ></script>
<script>
Declaring JSON data structures directly
var myjsonobject = {"Bindings": [
{"Ircevent": "Privmsg", "Method": "Newuri", "regex": "^http://.*"},
{"Ircevent": "Privmsg", "Method": "Deleteuri", "regex": "^delete.*"},
{"Ircevent": "Privmsg", "Method": "Randomuri", "regex": "^random.*"}
]
};
Declare a string to compare the difference between JSON text and our normal text
var normalstring= ' [{persons:[{name: "Jordan", Sex: "M", Age: "{"}, {name: "Bryant", Sex: "M", Age: "A"}, {name: "McGrady", Sex: "M", Age: "27"}]}] ';
var jsontext= ' [{persons ']: [{' Name ': ' Jordan ', ' sex ': ' m ', ' Age ': '} ', {' name ': ' Bryant ', ' sex ': ' m ', ' Age ': ' '}, {' Name ":" McGrady "," Sex ":" M "," Age ":" 27 "}]}] ';

Call the Eval function into a JSON object,
var Mye = eval (normalstring);
Document.writeln (Mye ' <br><br> ');
Convert a JSON object to a string
var text = json.stringify (Mye);
Compare the converted JSON text to the declared text difference
Document.writeln (' converted JSON text: ' text ' <br><br> declared JSON-formatted text ' Jsontext ' <br><br> declared plain text ' Normalstring ' <br><br> ');

It's better to use JSON parsing when security is more important. JSON parsing only recognizes JSON text and it is more secure, and the following call JSON's parse function generates a JSON data structure for text conversion
var myData = Json.parse (Jsontext);

Document.writeln (myData ' <br><br> ');

The following is a check on the JSON object's additions and deletions

Declaring a JSON object

var jsonobj2={persons:[{name: "Jordan", Sex: "M", Age: "N"}, {name: "Bryant", Sex: "M", Age: "A"}, {name: "McGrady", Sex: "M ", Age:" 27 "}]};

var persons=jsonobj2.persons;
var str= "";

var person={name: "Yaoming", Sex: "M", Age: "26"};
The following is the operation of the JSON object, which removes the annotation to view the results of the operation
JsonObj2.persons.push (person);//array last add a record
JsonObj2.persons.pop ()//Delete last item
JsonObj2.persons.shift ();//Delete first item
JsonObj2.persons.unshift (person);//array front plus a record as long as the appropriate JavaScript method can be used in the JSON object array! So there is another way to splice () CRUD Operations! Delete
JsonObj2.persons.splice (0,2)//start position, delete number
Replace do not delete
var self={name: "Tom", Sex: "M", Age: "24"};
var brother={name: "Mike", Sex: "M", Age: "29"};
JsonObj2.persons.splice (1,0,self,brother,self);//start position, delete number, insert Object
Replace and delete
JsonObj2.persons.splice (0,1,self,brother);//start position, delete number, insert Object

for (Var i=0;i<persons.length;i) {var cur_person=persons[i]; Str =cur_person.name "' Sex are ' cur_person.sex" and age I S "Cur_person.age" <br><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.