html页面如何转化为json,如何在HTML页面上显示JSON文件

HTML代码:

只要保持在一个变量的JSON数据(或)单独的文件作为'FamilyAndFriends.json'。

var data = {

"people":[{"fname":"Adam",

"lname":"Schmitt",

"relation":"Brother",

"relationid":"1"

},

{"fname":"Andrew",

"lname":"Name",

"relation":"Brother",

"relationid":"1"

},

{"fname":"Jenna",

"lname":"Name",

"relation":"Wife",

"relationid":"2"

},

{"fname":"Gary",

"lname":"Name",

"relation":"Father",

"relationid":"3"

},

{"fname":"Judy",

"lname":"Name",

"relation":"Mother",

"relationid":"4"

},

{"fname":"Justin",

"lname":"Name",

"relation":"Friend",

"relationid":"5"

},

{"fname":"Billy",

"lname":"Name",

"relation":"Friend",

"relationid":"5"

},

{"fname":"Robbie",

"lname":"Name",

"relation":"Friend",

"relationid":"5"

},

{"fname":"Dylan",

"lname":"Name",

"relation":"Friend",

"relationid":"5"

},

{"fname":"Zach",

"lname":"Name",

"relation":"Friend",

"relationid":"5"

}]

};

实例外部JSON使用javascript:

var link;

for (var t = 0; t < data.people.length; t++){

link = document.createElement("a"+t);

var ulist = document.getElementById("list");

var newItem = document.createElement("li");

newItem.appendChild(link);

ulist.appendChild(newItem);

link.textContent = 'First Name: '+data.people[t].fname + ', Last Name: ' +data.people[t].lname;

link.setAttribute('data-ind', t); // ADDED A DATA ATTRIBUTE TO STORE A INDEX VALUE

if (typeof window.addEventListener === 'function'){

(function() {

link.addEventListener('click', function(){

var indexId = this.getAttribute('data-ind'); //GETTING DATA ATTRIBUTE VALUE

indexId = parseInt(indexId); //PARSE FROM STRING TO INTEGER

alert(data.people[indexId].relation); // YOU WILL GET THE ACTUAL RELATION VALUE.

});

})(link);

}

}

结果就会像下面,当您单击第一个列表:

var request = new XMLHttpRequest();

request.open('GET', '/my/url', true); // YOUR EXTERNAL JSON URL

request.onload = function() {

if (request.status >= 200 && request.status < 400) {

// Success!

var data = JSON.parse(request.responseText);

var link;

for (var t = 0; t < data.people.length; t++){

link = document.createElement("a"+t);

var ulist = document.getElementById("list");

var newItem = document.createElement("li");

newItem.appendChild(link);

ulist.appendChild(newItem);

link.textContent = 'First Name: '+data.people[t].fname + ', Last Name: ' +data.people[t].lname;

link.setAttribute('data-ind', t); // ADDED A DATA ATTRIBUTE TO STORE A INDEX VALUE

if (typeof window.addEventListener === 'function'){

(function (_td) {

link.addEventListener('click', function(){

var indexId = this.getAttribute('data-ind'); //GETTING DATA ATTRIBUTE VALUE

indexId = parseInt(indexId); //PARSE FROM STRING TO INTEGER

alert(data.people[indexId].relation); // YOU WILL GET THE ACTUAL RELATION VALUE.

});

})(link);

}

}

} else {

// We reached our target server, but it returned an error

}

};

request.onerror = function() {

// There was a connection error of some sort

};

request.send();

使用JavaScript内部JSON例

Brother

摇滚吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值