index1.json:
{
"A":[
{"name":"zhangsan"},
{"sex":"man"},
{"age":23}
],
"B":[3,4,1,23,54]
}
<script type="text/javascript">
$.ajax({
url:"../data/index1.json",
type:"get",
dataType:"json",
success:function(data){
//A
alert(data);//[object object]对象集合
alert(typeof data);//object
alert(data.A);//[object object],[object object],[object object],
alert(data.A[0]);//[object object]
alert(typeof data.A);//object
alert(data.A.name);//undefined
alert(data.A.length);//3
alert(data.A[0].name);//zhangsan
alert(data.A[1].name);//undefined,之所以出现undefined,是因为json中[]内的每个{}也是一个对象数组了
alert(data.A[1].sex);//man
$('#json').html(data.A[0].name);//zhangsan
for(var i in data.A){
for(var j in data.A[i]){
document.write(i);//0,1.2
document.write(j);//name,sex,age
document.write(data.A[i]);//[object object],[object object],[object object]
document.write(data.A[j]);//undefine dundefine undefine
document.write(data.A[i][j]);//zhangsan,man,23
document.write(j+":"+data.A[i][j]+"<br/>");//打印data.A所有数据
}
}
//B
alert(data.B);//3,4,1,23,54
alert(data.B[0]);//3
alert(typeof data.B);//object
for(var i=0;i<data.B.length;i++){
document.write(i+":"+data.B[i]+"<br/>")
}
for(var i in data.B){
document.write(i+":"+data.B[i]+"<br/>")
}//以上两种循环方式结果一样,打印data.B所有数据
}
})
</script>
index2.json:
{
"ecgcode":[3,4,1,23,54],
"ecg":
{
"id":1,
"createDate":null,
"updateDate":1476172420000,
"uploadTime":1474445600000
}
}
<script type="text/javascript">
$.ajax({
url:"../data/index2.json",
type:"get",
dataType:"json",
success:function(data){
document.write(data.ecg);//[object Object]
document.write(data.ecg.id);//1
for(var i in data.ecg){
document.write(i+":"+data.ecg[i]+"<br/>");//打印data.ecg所有数据
}
}
})
</script>
index3.json:
[
{
"_id": "58008a1b62ee553fefe76363",
"index": 0,
"tags": ["culpa","consequat","irure","aliqua","elit","amet","exercitation"],
"friends": [
{"id": 0,"name": "Lana Mathis"},
{"id": 1,"name": "Molina Wade"},
{"id": 2,"name": "Finley Potter"}
],
"greeting": "Hello, Anita Kerr! You have 3 unread messages.",
"favoriteFruit": "strawberry"
},
{
"_id": "58008a1be00be2f90fda3738",
"index": 1,
"tags": ["ullamco","laboris","nulla","pariatur","non","excepteur","aliqua"],
"friends": [
{"id": 0,"name": "Malinda Bridges"},
{"id": 1, "name": "Carrillo Le"},
{"id": 2,"name": "Bates Ortega"}
],
}
]
<script type="text/javascript">
$.ajax({
url:"../data/index3.json",
type:"get",
dataType:"json",
success:function(data){
alert(data);//[object Object]*5,有5个{}
alert(data[0]);//[object Object]
alert(data[0].index);//0
alert(data[1].tags);//打印所有tags值
alert(data[1].friends);//[object pbject],[object pbject],[object pbject]
for(var i in data[1].friends){
for(var j in data[1].friends[i]){
document.write(j+":"+data[1].friends[i][j]+"<br/>");//打印data[4].friends数据
}
}
for(var i in data){
for(var j in data[i]){
document.write(i);//0,1,2,3,4
document.write(j);//所有键值队的键
document.write(data[i]);//[object pbject],[object pbject],[object pbject]
document.write(data[j]);//undefine undefine undefine
document.write(data[i][j]);//zhangsan,man,23
document.write(j+":"+data[i][j]+"<br/>");//打印data所有数据
}
}
}
})
</script>