JSON
1.概念
虽然它是基于 JavaScript 语法,但它独立于JavaScript,一个 JSON 对象可以被储存在它自己的文件中,这基本上就是一个文本文件,扩展名为 .json
, 还有 MIME type 用于 application/json
.
2.作用
作为对象:用于解读 JSON 中的数据
作为字符串:用于通过网络传输 JSON 数据
3.结构
{
"squadName" : "Super hero squad",
"homeTown" : "Metro City",
"formed" : 2016,
"secretBase" : "Super tower",
"active" : true,
"members" : [
{
"name" : "Molecule Man",
"age" : 29,
"secretIdentity" : "Dan Jukes",
"powers" : [
"Radiation resistance",
"Turning tiny",
"Radiation blast"
]
},
{
"name" : "Madame Uppercut",
"age" : 39,
"secretIdentity" : "Jane Wilson",
"powers" : [
"Million tonne punch",
"Damage resistance",
"Superhuman reflexes"
]
},
{
"name" : "Eternal Flame",
"age" : 1000000,
"secretIdentity" : "Unknown",
"powers" : [
"Immortality",
"Heat Immunity",
"Inferno",
"Teleportation",
"Interdimensional travel"
]
}
]
}
4.使用
1.访问属性
superHeroes.hometown
superHeroes["active"]
2.对象和字符串之间的转换