JSON learning JSON in JavaScript detailed usage instructions _json

Source: Internet
Author: User
Copy Code code as follows:

<script type= "Text/javascript" src= "Scripts/json/json2.js" ></script>
<script type= "Text/javascript" src= "Scripts/json/jsonmessage.js" ></script>

first, use JSON to create an object in JavaScript
JS Code
Copy Code code as follows:

Create an empty object
var jsonobject = {}
To create a new object
var jsonobject = new Object ()
Creates an object that contains a property, with the name string and the age as an integral type
var jsonobject = {
"Name": "Kevin",
"Age": 23
}

Similar to Java, we can use the dot (.) The operator gets the properties of the object.
Copy Code code as follows:

var jsonobject = {
"Name": "Kevin",
"Age": 24,
};

Alert ("Jsonobject.name:" +jsonobject.name);
Alert ("Jsonobject.age:" +jsonobject.age);

Ii. Creating an Array object in JavaScript using JSON

Creates a student object containing two array objects, each containing the properties of the student object.

Copy Code code as follows:

var student = {
First Array Object class
"Class": [
{
"Name": "Kevin",
"ClassName": "Java",
"Age": 23
},
{
"Name": "Yang",
"ClassName": "Java",
"Age": 24
}
],
A 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);
}

using JSON to create messages in JavaScript

Copy Code code 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 is 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 Code code as follows:

Expression has browser-compatible 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 one is fixed.
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 '),
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.