CopyCode The Code is as follows: <SCRIPT type = "text/JavaScript" src = "scripts/JSON/json2.js"> </SCRIPT>
<SCRIPT type = "text/JavaScript" src = "scripts/JSON/jsonmessage. js"> </SCRIPT>
1. Create an object using JSON in Javascript
JS CodeCopy codeThe Code is as follows: // create an empty object
VaR jsonobject = {}
// Create a new object
VaR jsonobject = new object ()
// Create an object that contains attributes. The name is a string and the age is an integer.
VaR jsonobject = {
"Name": "Kevin ",
"Age": 23
}
Similar to Java, we can use the dot (.) operator to obtain the attributes of an object.Copy codeThe Code is as follows: var jsonobject = {
"Name": "Kevin ",
"Age": 24,
};
Alert ("jsonobject. Name:" + jsonobject. Name );
Alert ("jsonobject. Age:" + jsonobject. Age );
2. Create an array object using JSON in Javascript
Create a student object that contains two array objects, each of which contains the attributes of the student object.
Copy code The Code is as follows: var student = {
// The first array object class
"Class ":[
{
"Name": "Kevin ",
"Classname": "Java ",
"Age": 23
},
{
"Name": "yang ",
"Classname": "Java ",
"Age": 24
}
],
// The second array object
"Score ":[
{
"Name": "Shower ",
"Score": 100
},
{
"Name": "zheng ",
"Score": 100
}
]
}
VaR I = 0;
For (I = 0; I <student. Class. length; I ++ ){
Alert ("student. class [" + I + "]. Name =>" + student. class [I]. Name );
Alert ("student. class [" + I + "]. classname =>" + student. class [I]. classname );
Alert ("student. class [" + I + "]. Age =>" + student. class [I]. Age );
}
For (I = 0; I <student. Score. length; I ++ ){
Alert ("student. Score [" + I + "]. Name =>" + student. Score [I]. Name );
Alert ("student. Score [" + I + "]. Score =>" + student. Score [I]. Score );
}
3. Create a message in Javascript using JSON
Copy code The Code is as follows: // create a student object
VaR student = {
"Math ":[{
"Name": "Kevin ",
"Mark": 70,
"Age": 23
},{
"Name": "smart ",
"Mark": 40,
"Age": 25
}
],
"Science ":[{
"Name": "kevin2 ",
"Mark": 70,
"Age": 23
},{
"Name": "smart2 ",
"Mark": 40,
"Age": 25
}
]
}
// Print array value
VaR I = 0;
VaR array = new array ();
For (I = 0; I <student. Math. length; I ++ ){
Array. Push (student. Math [I]. Name );
Array. Push (student. Math [I]. Mark );
Array. Push (student. Math [I]. Age );
}
For (I = 0; I <student. Science. length; I ++ ){
Array. Push (student. Science [I]. Name );
Array. Push (student. Science [I]. Mark );
Array. Push (student. Science [I]. Age );
}
Alert ("array =>" + array );
// This method produce a JSON text from a javascript value.
// This method converts a javascript value to a JSON string
alert ("array. tojsonstring () => "+ array. tojsonstring ();
alert ("string. parsejson => "+ array. tojsonstring (). parsejson ();
var data2 = array. tojsonstring (). parsejson ();
If (data2 instanceof array) {
alert ("array");
}
copy the Code the code is as follows: // The expression has browser compatibility issues
// var Cx =/[\ u0000 \ u00ad \ u0600-\ u0604 \ u070f \ u17b4 \ u17b5 \ u200c-\ u200f \ u2028- \ u202f \ u2060-\ u206f \ ufeff \ ufff0-\ Uffff]/g,
// escapable =/[\\\ "\ x00-\ x1f \ x7f-\ x9f \ u00ad \ u0600-\ u0604 \ u070f \ u17b4 \ u17b5 \ u200c -\ u200f \ u2028-\ u202f \ u2060-\ u206f \ ufeff \ ufff0-\ Uffff]/g,
// This is the corrected
var Cx = new Regexp ('/[\ u0000 \ u00ad \ u0600-\ u0604 \ u070f \\ u17b4 \ u17b5 \ u200c-\ u200f \ u2028-\ u202f \ u2060-\ u206f \ ufeff \ ufff0-\ Uffff]/G '),
escapable = new Regexp ('/[\\\\\\ "\\ x00-\ x1f \ x7f-\ x9f \ u00ad \ u0600 -\ \ u0604 \ u070f \ u17b4 \ u17b5 \ u200c-\ u200f \ u2028-\ u202f \ u2060-\ u206f \ ufeff \ ufff0 -\ \ Uffff]/G '),