4.JSON
4.1 What is JSON?
JavaScript Object Notation (JavaScript Object Notation) referred JSON, a lightweight data-interchange format. Although it is based on the object literal notation of JavaScript, it is language-independent, unrelated said here meant that no dependencies, not to say that can only be used in the js, and then other languages also can be used.
var student = {
"name": '张三',
"age": 29,
"data":[1,2,3]
};
4.2 JSON syntax
JSON key-value pairs appear in the form of:
var jsonObject = `{"key1":"value1","key2":"value2"}`
Key: Name value with double quotation marks: value of any type may be used between the key and values: spaced.
The type of value:
数字(整数或浮点数)
字符串(在双引号中)
逻辑值(true 或 false)
数组(在方括号中)
对象(在花括号中)
null
一个具体的函数或者匿名函数
4.3 JSON uses
JSON syntax for storing and exchanging text messages. Similar to XML. For data transfer between systems.
4.4 JSON advantage
JSON is a lightweight data interchange format JSON text is independent of the language. JSON self-descriptive and easier to read. JSON is smaller than XML, faster, and easier to resolve. JSON formatted data is inherently a Javascript object.
4.5 JSON converting between strings and objects js
4.5.1 General JSON
An object used to store information.
//1.定义json对象
var employee = `{
"id":1,
"name":"刘备",
"age":28,
"sex":'男'
}`
// 转成js对象才能通过 对象.属性的形式拿到对应的值
employee = JSON.parse(employee);
//2.获取对象中的数据
console.log(employee.id,employee.name,employee.age);
4.5.2 with an array of JSON
[] Indicates an array in json the employee record contains three objects:
//>>3.定义json数组
var employees = `[
{"id":1,"name":'刘备',"age":28,"gender":'男'},
{"id":2,"name":'关羽',"age":27,"gender":'男'},
{"id":3,"name":'张飞',"age":26,"gender":'男'}
]`;
employee = JSON.parse(employee);
for(var i=0; i<employee.length;i++){
console.log(employee.id, employee.age, employee.gender)
}
Screw classroom video lessons Address: http://edu.nodeing.com