您好,登錄后才能下訂單哦!
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。