您好,登錄后才能下訂單哦!
①
JSON數據如下,是一個嵌套JSON:
{"comments":[{"content":"很不錯嘛","id":1,"nickname":"納尼"},{"content":"喲西喲西","id":2,"nickname":"小強"}]}
function loadInfo() {
$.getJSON("loadInfo", function(data) {
$("#info").html("");//清空info內容
$.each(data.comments, function(i, item) {
$("#info").append(
"<div>" + item.id + "</div>" +
"<div>" + item.nickname + "</div>" +
"<div>" + item.content + "</div><hr/>");
});
});
}
loadinfo是請求的地址,function(data){...}就是在請求成功后的回調函數,data封裝了返回的JSON對象,在下面的$.each(data.comments,function(i,item){...})方法中data.comments直接到達JSON數據內包含的JSON數組:
[{"content":"很不錯嘛","id":1,"nickname":"納尼"},{"content":"喲西喲西","id":2,"nickname":"小強"}]
②
如果返回的JSON數據比較復雜,則只需多些$.each()進行遍歷即可,嘿嘿。例如如下JSON數據:
{"comments":[{"content":"很不錯嘛","id":1,"nickname":"納尼"},{"content":"喲西喲西","id":2,"nickname":"小強"}],"content":"你是木頭人,哈哈。","infomap":{"性別":"男","職業":"程序員","博客":"http:\/\/www.cnblogs.com\/codeplus\/"},"title":"123木頭人"}
functionloadInfo() {
$.getJSON("loadInfo", function(data) {
$("#title").append(data.title+"<hr/>");
$("#content").append(data.content+"<hr/>");
//jquery解析map數據
$.each(data.infomap,function(key,value){
$("#mapinfo").append(key+"----"+value+"<br/><hr/>");
});
//解析數組
$.each(data.comments, function(i, item) {
$("#info").append(
"<div>"+item.id +"</div>"+
"<div>"+item.nickname +"</div>"+
"<div>"+item.content +"</div><hr/>");
});
});
}
③直接解析一個json字符串
var result={"getMeters":[{"Mid":"34","Mname":"","Tid":"5416685","Tname":"5號樓","Tcldid":"11","Unicode":"152"},{"Mid":"35","Mname":"","Tid":"5416685","Tname":"5號樓","Tcldid":"12","Unicode":"152"}]}
var getCode=new Array();
var dataobj = eval("(" + result.value + ")"); //轉換為json對象
$.each(dataobj.getMeters, function (i, item) {
getCode.push(item.Unicode);
});
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。