Home > Web Front-end > JS Tutorial > javascript json2 usage_javascript skills

javascript json2 usage_javascript skills

WBOY
Release: 2016-05-16 18:32:09
Original
1044 people have browsed it
Copy code The code is as follows:


<script> <br>//Declare the json data structure directly <br>var myJSONObject = {"bindings": [ <br>{"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"}, <br>{"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete .*"}, <br>{"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"} <br>] <br>}; <br>// To declare a string, you can compare the difference between json text and our normal text <br>var normalstring='[{persons:[{name:"jordan",sex:"m",age:"40"}, {name: "bryant",sex:"m",age:"28"}, {name:"McGrady",sex:"m",age:"27"} ]}]'; <br>var jsontext='[{ "persons":[{"name":"jordan","sex":"m","age":"40"}, {"name":"bryant","sex":"m","age ":"28"}, {"name":"McGrady","sex":"m","age":"27"} ]}]'; <br><br>//Call the eval function to convert to json object, <br>var myE = eval(normalstring); <br>document.writeln(myE '<br><br>'); <br>//Convert json object to string <br>var text = JSON.stringify(myE); <br>//Compare the difference between the converted json text and the declared text<br>document.writeln('Converted json text:' text '<br><br> Declared json format text ' jsontext '<br><br> Declared normal format text ' normalstring '<br><br>'); <br><br>//Use when security is more important JSON parsing is better. JSON parsing will only recognize JSON text and it is more secure. The parse function of json is called below to convert the text data to generate a json data structure <br>var myData = JSON.parse(jsontext); <br><br>document.writeln(myData '<br><br>'); <br><br>//The following is the operation of adding, deleting, checking and modifying the json object<br><br>//Declaring the json object<br><br>var jsonObj2= {persons:[{name:"jordan",sex:"m",age:"40"}, {name:"bryant",sex:"m",age:"28"}, {name:"McGrady" ,sex:"m",age:"27"} ]}; <br><br>var persons=jsonObj2.persons; <br>var str=""; <br><br>var person={name: "yaoMing",sex:"m",age:"26"}; <br>//The following are the operations of the json object, remove the comments to view the operation results<br>//jsonObj2.persons.push(person);/ /Add a record to the end of the array<br>//jsonObj2.persons.pop();//Delete the last item<br>//jsonObj2.persons.shift();//Delete the first item<br>jsonObj2.persons .unshift(person);//Add a record at the front of the array, as long as the method is suitable for Javascript, it can be used in the array of JSON objects! So there is another method splice() for crud operation! //Delete<br>//jsonObj2.persons.splice(0,2);//Start position, delete number<br>//Replace without deletion<br>var self={name:"tom",sex: "m",age:"24"}; <br>var brother={name:"Mike",sex:"m",age:"29"}; <br>jsonObj2.persons.splice(1,0, self,brother,self);//Starting position, delete number, insert object<br>//Replace and delete<br>//jsonObj2.persons.splice(0,1,self,brother);//Starting position , delete the number, insert the object <br><br>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><br>"; } <br>document.writeln(str); <br>//Convert to json text<br>var myjsonobj = JSON.stringify(jsonObj2 ); <br>document.writeln(myjsonobj); <br></script>
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template