首页 > 代码库 > JavaScript JSON语法
JavaScript JSON语法
1.创建JSON
JSON数距的是以键值对存储。
语法:
var JSOM名 ={"name":"张三","sex":"女","age":19};
var JSOM名 ={"student":[{"name":"张三","sex":"男","age":18},{"name":"李四","sex":"女","age":19},{"name":"王五","sex":"女","age":19}]};
var JSOM名 ={"student":{"name":"张三","sex":"男","age":18}};
2.调用JSON
(1)
var JSOM名 ={"name":"张三","sex":"女","age":19};
调用:alert(JSOM名.name);
(2)
var JSOM名 ={"student":[{"name":"张三","sex":"男","age":18},{"name":"李四","sex":"女","age":19},{"name":"王五","sex":"女","age":19}]};
调用:
var arr = JSOM名.student;
for(i=0;i<arr.length;i++)
{
alert(arr[i].age+arr[i].name+arr[i].sex)
}
(3)
var JSOM名 ={"student":{"name":"张三","sex":"男","age":18}};
调用:
alert(JSOM名.student.name);
JavaScript JSON语法