JSON是轻量级的数据交换格式。作用:易于人阅读和编写,易于机器解析和生成。
-
名称/值队
单个键值对会比等效的纯文本格式占用更多空间,但是JSON可以将多个键值对串起来。
{“firstname”:”Jack”,”Lastname”:”Liu”} -
数组
当需要表示一组值时,JSON可以提高可读性,减少复杂性。
{
“people”:[
{“firstname”:”Jack”,”Lastname”:”Liu”},
{“firstname”:”Jack1”,”Lastname”:”Liu1”},
{“firstname”:”Jack2”,”Lastname”:”Liu2”} ],
“country”:”china”,
“city”:”beijing”
} -
数据操作
页面将JSON赋值给一个变量,然后读取数据。var info = {
“people”:[
{“firstname”:”Jack”,”Lastname”:”Liu”},
{“firstname”:”Jack1”,”Lastname”:”Liu1”},
{“firstname”:”Jack2”,”Lastname”:”Liu2”} ],
“country”:”china”,
“city”:”beijing”
}读取数据
info.people[0].firstname
info.country修改数据
info.people[0].firstname = “lucy”;
info.country = “canada”;