目录
概念: JavaScript Object Notation JavaScript对象表示法
概念: JavaScript Object Notation JavaScript对象表示法
Person p = new Person();
p.setName("张三");
p.setAge(23);
p.setGender("男");
var p = {"name":"张三","age":23,"gender":"男"};
* json现在多用于存储和交换文本信息的语法
* 进行数据的传输
* JSON 比 XML 更小、更快,更易解析。
语法:
1. 基本规则
* 数据在名称/值对中:json数据是由键值对构成的
* 键用引号(单双都行)引起来,也可以不使用引号
* 值得取值类型:
1. 数字(整数或浮点数)
2. 字符串(在双引号中)
3. 逻辑值(true 或 false)
4. 数组(在方括号中) {"persons":[{},{}]}
5. 对象(在花括号中) {"address":{"province":"陕西"....}}
6. null
* 数据由逗号分隔:多个键值对由逗号分隔
* 花括号保存对象:使用{}定义json 格式
* 方括号保存数组:[]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="js/jquery-3.3.1.min.js"></script>
<script>
//json:javascript object notatin:javascript 对象表示法
//json的基本结构:{key:value}
//key:可以加上双引号或者单引号或者不加
//value:可以是数字、布尔、字符串、数组、对象
/* User user = new User();
user.setUSerName("zhangsan");*/
var user = {username: "张三", age: 23, sex: true};
// alert(user.username);
var user1 = {"username": "张三", "age": 23, "sex": true};
// alert(user1.sex);
var users = {
users: [
{"username": "张三", "age": 23, "sex": true},
{"username": "李四", "age": 32, "sex": false},
{"username": "王五", "age": 27, "sex": true}
]
};
// alert(users.users[1].username);
var users1 = [
{"username": "张三", "age": 23, "sex": true},
{"username": "李四", "age": 32, "sex": false},
{"username": "王五", "age": 27, "sex": true}
];
alert(users1[2].age);
</script>
</head>
<body>
</body>
</html>
2. 获取数据:
1. json对象.键名
2. json对象["键名"]
3. 数组对象[索引]
4. 遍历
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="js/jquery-3.3.1.min.js"></script>
<script>
//json:javascript object notatin:javascript 对象表示法
//json的基本结构:{key:value}
//key:可以加上双引号或者单引号或者不加
//value:可以是数字、布尔、字符串、数组、对象
//遍历value为数组类型,获得的key是索引值index,获取value值通过json[index]
/* User user = new User();
user.setUSerName("zhangsan");*/
var user = {username: "张三", age: 23, sex: true};
// alert(user.username);
/* for (var key in user ){
// alert(key);//username
// alert(user.key);//user."username"
alert(key+":"+user[key]);
}*/
var user1 = {"username": "张三", "age": 23, "sex": true};
// alert(user1.sex);
/* for (var key in user1 ){
// alert(key);//username
// alert(user.key);//这样的方式获取不行。因为相当于 user."username"
alert(key+":"+user1[key]);
}*/
var users = {
users: [
{"username": "张三", "age": 23, "sex": true},
{"username": "李四", "age": 32, "sex": false},
{"username": "王五", "age": 27, "sex": true}
]
};
// alert(users.users[1].username);
/* for (var key in users) {
/!* alert(key);
alert(users[key]);*!/
var userArry = users[key];
for (var key in userArry) {
alert(key);
alert(userArry[key].username)
}
}*/
/* var userList = users.users;
for (var key in userList) {
alert(userList[key].username)
}*/
var users1 = [
{"username": "张三", "age": 23, "sex": true},
{"username": "李四", "age": 32, "sex": false},
{"username": "王五", "age": 27, "sex": true}
];
for (var key in users1) {
alert(key)
alert(users1[key].age)
}
var per = [{
"usename": "杨紫",
"phone": "12221123",
"birthday": 1568171538739,
"email": null,
"password": null,
"repassword": null
}, {
"usename": "李现",
"phone": "1231123",
"birthday": 1568171538739,
"email": null,
"password": null,
"repassword": null
}, {
"usename": "鹿晗",
"phone": "123321123",
"birthday": 1568171538739,
"email": null,
"password": null,
"repassword": null
}, {
"usename": "关晓彤",
"phone": "13331123",
"birthday": 1568171538739,
"email": null,
"password": null,
"repassword": null
}]
</script>
</head>
<body>
</body>
</html>