歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Json讀取的方式辨析

讀取一:


$.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);
                        JavaScriptSerializer jss = new JavaScriptSerializer();
                        Response.Write(jss.Serialize(points));

 

看到沒有。它是TB_MonitoringPoint的一個數組!所以當然要用$.each!

Copyright © Linux教程網 All Rights Reserved