Home > Web Front-end > JS Tutorial > body text

Preliminary learning and understanding of json2.js_json

WBOY
Release: 2016-05-16 18:01:24
Original
1079 people have browsed it

Initial learning and understanding of json2.js
1.) The download address of the js is: http://www.json.org/json2.js
2.) Quote the script on the page:
3.) Example Demonstration 1:

Copy code The code is as follows:

//Declare the json data structure directly
var myJSONObject = {"bindings": [
{"ircEvent": "PRIVMSG ", "method": "newURI", "regex": "^http://.*"},
{"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": " ^delete.*"},
{"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
]
};

In this example, an object is created that contains only one member "bindings". "bindings" is an array containing 3 objects, and each object has 3 members: "ircEvent", "method" and "regex".
These members can be obtained using "." or subscript operations.
For example: myJSONObject.bindings[0].method // "newURI"
myJSONObject.bindings[1].deleteURI // "newURI"
//Declaration string, you can compare the json text with ours The difference between 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"} ]}]';
We can use the eval() function to call the JavaScript compiler to convert JSON text into an object. Because JSON is an exact subset of JavaScript, the compiler can correctly parse the JSON text and then generate an object structure.
//Call the eval function to convert to a json object,
var myE = eval(normalstring);
//Convert the json object to a string
var text = JSON.stringify(myE);
//Compare the difference between the converted json text and the declared text
document.writeln('Converted json text:' text '

Declared json format text' jsontext '< br>
Declared normal format text 'normalstring '

');
The result is as follows:
Converted json text: [{"persons":[{"name ":"jordan","sex":"m","age":"40"},{"name":"bryant","sex":"m","age":"28"},{ "name":"McGrady","sex":"m","age":"27"}]}]
Declared json format text [{"persons":[{"name":"jordan" ,"sex":"m","age":"40"},{"name":"bryant","sex":"m","age":"28"},{"name":" McGrady","sex":"m","age":"27"}]}]
The plain text of the declaration [{persons:[{name:"jordan",sex:"m",age: "40"}, {name:"bryant",sex:"m",age:"28"}, {name:"McGrady",sex:"m",age:"27"} ]}]
Summary: The converted json text and the declared json format text content are the same.
//When security is more important, it is better to use JSON parsing. 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
var myData = JSON.parse(jsontext);
The complete file is as follows (difference: myJSONObject , jsontext, normalstring):
Copy code The code is as follows:

<%@ page language="java" pageEncoding="UTF-8"%>






<script> <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>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>//Call the eval function to convert to a json object, <br>var myE = eval(normalstring); <br>//Convert the json object to a 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 string 'normalstring '<br><br>'); <br>//JSON parsing<br>var myData = JSON.parse(jsontext); <br></script&gt ; <BR></body> <br></html> <br> </div> <br>4.) Example demonstration two: <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="4274" class="copybut" id="copybut4274" onclick="doCopy('code4274')"><u>Copy code </u></a></span> The code is as follows: </div> <div class="codebody" id="code4274"> <br>// The following is the operation of adding, deleting, checking and modifying json objects <br><%@ page language="java" pageEncoding="UTF-8"%> <br><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <br><html> <br><head> <br><script type="text/javascript" src="js/json2.js"></ script> <br></head> <br><body> <br><script> <br>//Declare json object<br>var jsonObj2={persons:[ <br>{name:"jordan ",sex:"m",age:"40"}, <br>{name:"bryant",sex:"m",age:"28"}, <br>{name:"McGrady",sex: "m",age:"27"} <br>]}; <br>var persons=jsonObj2.persons; <br>var str=""; <br>var person={name:"yaoMing",sex: "m",age:"26"}; <br>//The following is the operation of the json object, remove the comment to view the operation result<br>jsonObj2.persons.push(person);//Add a record at 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 to the front of the array A record can be used in an array of JSON objects as long as it is suitable for Javascript! So there is another method splice() for crud operation! //Delete<br>jsonObj2.persons.splice(0,2);//Start position, delete number<br>//Replace without deleting<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, number of deletions, insert object<br>//Replace and delete<br>jsonObj2.persons.splice(0,1,self,brother);//Start position, number of deletions , insert object <br>for(var i=0;i<persons.length;i ){ <br>var cur_person=persons[i]; <br>str =cur_person.name "'sex is " cur_person.sex " and age is " cur_person.age "<br><br>"; <br>} <br>document.writeln(str); <br>//Convert to json text<br>var myjsonobj = JSON.stringify (jsonObj2); <br>document.writeln(myjsonobj); <br>document.writeln(persons.length); <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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!