Json的名称为:JavaScript Object Notation(JavaScript对象表示法),它是一种轻量级的数据交换格式,而且相比较其它而言,它更容易被理解,具有着自我描述性
对于JSON来说它更倾向于JS里面的数组,也可以这么说,就是JS的量身定做
JSON的格式为以下
{"key":[{"key":"value"},{"key","value"},{"key,"value"}]}
这样的存储方式
举个栗子而言,每个学校拥有许多的教师和学生,学生拥有姓名和学号,老师拥有姓名,教工号,于是我们可以用以下这样的格式进行表示
{
"school": [
{
"teacher": [
{
"teacherName": "nameValue"
},
{
"teacherName": "nameValue"
}
]
},
{
"student": [
{
"studentName": "studentValue"
},
{
"studentName": "studentValue"
}
]
}
]
}
如果我们想把它打印出来,我们可以在JS中用以下的方式使用对其进行打印
<script type="text/javascript">
var json={
"school": [
{
"teacher": [
{
"teacherName": "nameValue"
},
{
"teacherName": "nameValue"
}
]
},
{
"student": [
{
"studentName": "studentValue"
},
{
"studentName": "studentValue"
}
]
}
]
};
console.log(json);
</script>
而在对于html中也会在相应的console中也显示出来(因为此时我们在JS中用的是console.log()的方法