JSON in JavaScript

Source: Internet
Author: User

Note: you only need to introduce the corresponding javascript on the front-end page to test the function.
Js Code
<Script type = "text/javascript" src ="Http://www.phpchina.com/scripts/json/json2.js> </script>
<Script type = "text/javascript" src = "scripts/json/jsonmessage. js"> </script>
1. Create an object using JSON in javascript
Js Code
// 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.
Js Code
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.
Js Code
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
Js Code
// 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 into 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 ");
}

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.