Home > Web Front-end > JS Tutorial > JSON detailed study JSON in JavaScript

JSON detailed study JSON in JavaScript

高洛峰
Release: 2016-11-25 11:30:53
Original
942 people have browsed it

Note: You only need to introduce the corresponding javascript into the front page to test the
Js code


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 containing attributes, where the name is a string and the age is Integer type
var JSONObject = {
"name":"kevin",
"age":23
}
Similar to java, we can get the properties of the object through the dot (.) operator.
Js code
var JSONObject = {
"name":"kevin",
"age":24,
};

alert("JSONObject.name:"+JSONObject.name);
alert("JSONObject.age :"+JSONObject.age);
2. Use JSON to create array objects in javascript
Create a Student object, which contains two array objects. Each array object contains the properties of the Student object.
Js code
var student = {                                                                            Class Name "age":23
},                                                                                        ],
//The second array object
"Score":[
                                   "name":"shower", "score":100                                                                           
                                                                                                                                                                                              (i=0;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 alert("student.Score["+i+"].name== =>"+student.Score[i].name);
alert("student.Score["+i+"].score===>"+student.Score[i].score);
}
3. Use JSON to create messages in javascript
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    array.push(Student.Math[i].name);   
    array.push(Student.Math[i].mark);   
    array.push(Student.Math[i].age);   
}   
  
for(i=0;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.  
//这个方法将一个JavaScript值转换为一个JSON字符串  
alert("array.toJSONString()==>"+array.toJSONString());   
alert("String.parseJSON==>"+array.toJSONString().parseJSON());   
var data2 = array.toJSONString().parseJSON();   
if(data2 instanceof Array){   
    alert("Array");   

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