以下示例為使用Ajax請(qǐng)求json數(shù)據(jù)并顯示在頁(yè)面HTML中的代碼,請(qǐng)求數(shù)據(jù)及json數(shù)據(jù)轉(zhuǎn)換形式供參考:
說(shuō)明:使用json.pares轉(zhuǎn)換為json對(duì)象
1.parse 用于從一個(gè)字符串中解析出json 對(duì)象。例如
var str='{"name":"cpf","age":"23"}'
經(jīng) JSON.parse(str) 得到:
Object: age:"23"
name:"cpf"
_proto_:Object
ps:單引號(hào)寫(xiě)在{}外,每個(gè)屬性都必須雙引號(hào),否則會(huì)拋出異常
2.stringify用于從一個(gè)對(duì)象解析出字符串,例如
var a={a:1,b:2}
經(jīng) JSON.stringify(a)得到:
“{“a”:1,"b":2}”
$.ajax({
url: '/ajax/getFloorManageByBuildingManageID',
type: 'post',
Data: {"building.id": id},
dataType: 'json',
success: function(data){
$('#floorManagerList').text(''); //將樓層內(nèi)容清空
var floor = "";
var obj = json.pares(data);
for(var i=0; i<data.length; i++){
floor += "<li class='bed-on'><div class='col-sm-8 pd-left'>"+obj[i].floorname+"</div>"+
"<div class='col-sm-4 bed-edit'><a data-toggle='modal' data-target='#myModal'>"+
"<i class='icon inconfont'></i></a>"+
"<a><i class='icon iconfont'></i></a>"+
"</div>"+
"</li>";
}
$('#floorManagerList').html(floor);
}
});
說(shuō)明:使用json.pares轉(zhuǎn)換為json對(duì)象
1.parse 用于從一個(gè)字符串中解析出json 對(duì)象。例如
var str='{"name":"cpf","age":"23"}'
經(jīng) JSON.parse(str) 得到:
Object: age:"23"
name:"cpf"
_proto_:Object
ps:單引號(hào)寫(xiě)在{}外,每個(gè)屬性都必須雙引號(hào),否則會(huì)拋出異常
2.stringify用于從一個(gè)對(duì)象解析出字符串,例如
var a={a:1,b:2}
經(jīng) JSON.stringify(a)得到:
“{“a”:1,"b":2}”