Json读取的方式辨析

2014-11-24 14:57:52 · 作者: · 浏览: 33

读取一:



$.ajax({
type:"GET",
dataType:"json",
data:{id:json[i].ID},
async:false,
url:"../../Ajax/CalculateLamps.aspx",
success:function(data){
redlamp+=parseInt(data[0]["red"],10);
greenlamp+=parseInt(data[0]["green"],10);
yellowlamp+=parseInt(data[0]["yellow"],10);
}
});



我们来看看CalculateLamps返回的是什么?


result =


"[{green:'" + NumGreenLamp.ToString() + "',red:'" + NumRedLamp.ToString() + "',yellow:'" + NumYellowLamp.ToString() + "'}]";



看到了吗?每次发一次请求,他就返回一次


"[{green:'" + NumGreenLamp.ToString() + "',red:'" + NumRedLamp.ToString() + "',yellow:'" + NumYellowLamp.ToString() + "'}]";



所以他每一次永远只返回一条,所以data[0]["red"]这种方法。




读取二:


$.getJSON(
"../../Ajax/GetPoints.aspx",
{ ID: item.value },
function(json, status) {
if (status == "success") {
if (json == null) {
alert("该部门没有监控点!");
}
else {
$.each(json, function(i) {


json[i].........


}


}


});


我们来看看GetPoints.aspx这个页面返回的是什么?



var points = (from p in dc.TB_MonitoringPoint
where p.CompanyID == intid
select p).Skip(0 * pageSize).Take(pageSize);
java scriptSerializer jss = new java scriptSerializer();
Response.Write(jss.Serialize(points));



看到没有。它是TB_MonitoringPoint的一个数组!所以当然要用$.each!