JSON数据格式
ex01
<scriptlanguage="JavaScript">
/*
*类对象,封闭成员属性
*classPeople{
*Stringfirstname
*Stringlastname
*Stringemail
*}
*
*Peoplepeople=newPeople();
*
*people.firstname
*people.lastname
*people.email
*
*/
/*
*json格式表示一个对象,键值相对
*格式:{key01:value01,key02:value02}
*
*/
varpeople={"firstname":"zhang","lastname":"sanfeng","email":"keni@com"}
//alert("people"+people)
//alert("people")
////输出peopel的first属性的值,即zhang
alert("名:"+people.firstname);
//输出peopel的lastname属性的值,即sanfeng
alert("姓:"+people.lastname);
//输出peopel的email属性的值,即keni@com
alert("电子邮箱:"+people.email);
</script>
ex02
<scriptlanguage="JavaScript">
/*
*json格式表示一个对象
*格式:[
*{key01:value01,key02:value02},
*{key01:value01,key02:value02}
*]
*
*/
//json数组集合,存放两个对象,每个对象有三个属性
varpeople=[
{"firstname":"zhang1","lastname":"sanfeng1","email":"keni1@com"},
{"firstname":"zhang2","lastname":"sanfeng2","email":"keni2@com"}
]
alert("对象长度"+people.length);//长度为2
//输出对象数组中第二个对象的lastname属性
alert("第二个对象的lastname属性:"+people[1].lastname);//输出zhang2
</script>
ex03
<scriptlanguage="JavaScript">
/*
*json格式表示一个对象
*格式:{key:
*[
*{key01:value01,key02:value02},
*{key01:value01,key02:value02}
*]
*}
*
*/
varpeople={"program":
[
{"firstname":"zhang1","lastname":"sanfeng1","email":"keni1@com"},
{"firstname":"zhang2","lastname":"sanfeng2","email":"keni2@com"}
]
}
alert(people.program.length);//输出program的长度为2
alert(people.program[1].lastname);//输出people对象中program属性中第二个对象的lastname
</script>
ex04
<scriptlanguage="JavaScript">
/*
*json格式表示一个对象
*格式:{key1:
*[
*{key01:value01,key02:value02},
*{key01:value01,key02:value02}
*],
*key2:
*[
*{key01:value01,key02:value02},
*{key01:value01,key02:value02}
*]
*}
*
*/
varpeople={"program1":
[
{"firstname":"zhang11","lastname":"sanfeng11","email11":"keni1@com"},
{"firstname":"zhang12","lastname":"sanfeng12","email12":"keni2@com"}
],
"program2":
[
{"firstname":"zhang21","lastname":"sanfeng21","email":"keni21@com"},
{"firstname":"zhang22","lastname":"sanfeng22","email":"keni22@com"}
],
"program3":
[
{"firstname":"zhang31","lastname":"sanfeng31","email":"keni31@com"},
{"firstname":"zhang32","lastname":"sanfeng32","email":"keni32@com"}
]
}
//输出people对象program3对象中第一个对象的last的值
alert(people.program3[0].lastname);
</script>
ex05
<scriptlanguage="JavaScript">
varpeople={
"username":"zhang",
"sex":"male",
"tel":{"phone":"110","cell":"13812345678"},
"address":[
{"city":"tieling","postcode":"110"},
{"city":"beijing","postcode":"100"}
]
}
alert(people.username);
alert(people.tel.cell);
alert(people.address[1].city);
alert(people.address[1].postcode)
</script>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。