from:http://blog.csdn.net/smartsmile2012/article/details/17316351

版权声明:本文为博主原创文章,未经博主允许不得转载。

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4.     <title>Ajax和getJSON获取后台普通Json数据和层级Json数据解析</title>  
  5.     <script src="JS/<a href="http://lib.csdn.net/base/22" class="replace_word" title="jQuery知识库" target="_blank" style="color:#df3434; font-weight:bold;">jquery</a>-1.8.0.min.js" type="text/<a href="http://lib.csdn.net/base/18" class="replace_word" title="JavaScript知识库" target="_blank" style="color:#df3434; font-weight:bold;">javascript</a>"></script>  
  6.     <script type="text/javascript">  
  7.         $(function () {  
  8.             //方式一 Ajax方式获取Json数据  
  9.             $.ajax({  
  10.                 url: 'jsondata.ashx?type=1',  
  11.                 type: 'GET',  
  12.                 dataType: 'json',  
  13.                 timeout: 1000,  
  14.                 cache: false,  
  15.                 beforeSend: LoadFunction, //加载执行方法    
  16.                 error: erryFunction,  //错误执行方法    
  17.                 success: succFunction //成功执行方法    
  18.             })  
  19.             function LoadFunction() {  
  20.                 $("#list").html('加载中...');  
  21.             }  
  22.             function erryFunction() {  
  23.                 alert("error");  
  24.             }  
  25.             function succFunction(tt) {                  
  26.                 var json = eval(tt); //数组     
  27.                 var tt = "";  
  28.                 $.each(json, function (index) {  
  29.                     //循环获取数据    
  30.                     var Id = json[index].id;  
  31.                     var Name = json[index].name;  
  32.                     var Age = json[index].age;  
  33.                     var Score = json[index].score;  
  34.                     tt += Id + "___" + Name + "___" + Age + "___" + Score + "<br>";  
  35.                 });  
  36.                 $("#list").html('');  
  37.                 $("#list").html(tt);  
  38.             }  
  39.   
  40.             //方式二 Json方式获取数据  
  41.             $.getJSON(  
  42.                 "jsondata.ashx?type=1",  
  43.                 function (data) {  
  44.                     //循环获取数据    
  45.                     var tt = "";  
  46.                     $.each(data, function (k, v) {  
  47.                         $.each(v, function (kk, vv) {  
  48.                             tt += kk + ":" + vv + "___";  
  49.                         });  
  50.                         tt += "<br/>";  
  51.                     });  
  52.                     $("#list2").html(tt);  
  53.                 }  
  54.             );  
  55.   
  56.             //方式三 Ajax方式获取Json层级数据  
  57.             $.ajax({  
  58.                 url: 'jsondata.ashx?type=3',  
  59.                 type: 'GET',  
  60.                 dataType: 'json',  
  61.                 timeout: 1000,  
  62.                 cache: false,  
  63.                 beforeSend: LoadFunction1, //加载执行方法    
  64.                 error: erryFunction1,  //错误执行方法    
  65.                 success: succFunction1 //成功执行方法    
  66.             })  
  67.             function LoadFunction1() {  
  68.                 $("#list3").html('加载中...');  
  69.             }  
  70.             function erryFunction1() {  
  71.                 alert("error");  
  72.             }  
  73.             function succFunction1(tt) {  
  74.                 var json = eval(tt); //数组     
  75.                 var tt = "";  
  76.                 $.each(json, function (index) {  
  77.                     //循环获取数据    
  78.                     var Id = json[index].id;  
  79.                     var Name = json[index].name;  
  80.                     var Age = json[index].age;  
  81.                     var Score = json[index].score;  
  82.                     tt += Id + "___" + Name + "___" + Age + "___";  
  83.                     $.each(Score, function (k, v) {  
  84.                         tt += k + ":" + v + "___";  
  85.                     })  
  86.                     tt += "<br/>";  
  87.                 });  
  88.                 $("#list3").html('');  
  89.                 $("#list3").html(tt);  
  90.             }  
  91.   
  92.             //方式四 Json方式获取层级数据  
  93.             $.getJSON(  
  94.                 "jsondata.ashx?type=3",  
  95.                 function (json) {  
  96.                     //循环获取数据    
  97.                     var tt = "";  
  98.                     $.each(json, function (index) {  
  99.                         //循环获取数据    
  100.                         var Id = json[index].id;  
  101.                         var Name = json[index].name;  
  102.                         var Age = json[index].age;  
  103.                         var Score = json[index].score;  
  104.                         tt += Id + "___" + Name + "___" + Age + "___";  
  105.                         $.each(Score, function (k, v) {  
  106.                             tt += k + ":" + v + "___";  
  107.                         })  
  108.                         tt += "<br/>";  
  109.                     });  
  110.                     $("#list4").html('');  
  111.                     $("#list4").html(tt);  
  112.                 }  
  113.             );  
  114.         });  
  115.     </script>  
  116. </head>  
  117. <body>  
  118.     <p>方式一</p>  
  119.     <ul id="list">  
  120.     </ul>  
  121.     ____________________________________  
  122.     <p>方式二</p>  
  123.     <ul id="list2">  
  124.     </ul>  
  125.     ____________________________________  
  126.     <p>方式三</p>  
  127.     <ul id="list3">  
  128.     </ul>  
  129.     ____________________________________  
  130.     <p>方式四</p>  
  131.     <ul id="list4">  
  132.     </ul>  
  133. </body>  
  134. </html>  

[csharp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <%@ WebHandler Language="C#" Class="jsondata" %>  
  2.   
  3. using System;  
  4. using System.Web;  
  5. using System.Web.Script.Serialization;  
  6. using System.IO;  
  7. using System.Text;  
  8. using System.Collections;  
  9. using System.Collections.Generic;  
  10. using System.Data;  
  11. using Newtonsoft.Json;  
  12.   
  13. public class jsondata : IHttpHandler {  
  14.   
  15.     public void ProcessRequest(HttpContext context)  
  16.     {  
  17.         context.Response.ContentType = "text/plain";  
  18.         context.Response.Cache.SetNoStore();  
  19.         string type = context.Request["type"];  
  20.         if (type=="1") //普通数据  
  21.         {  
  22.             List<Dictionary<String, String>> aa = new List<Dictionary<stringstring>>();  
  23.             for (int i = 0; i < 6; i++)  
  24.             {  
  25.                 Dictionary<String, String> aaa = new Dictionary<stringstring>();  
  26.                 aaa.Add("id""no" + i);  
  27.                 aaa.Add("name""张三" + i);  
  28.                 aaa.Add("age""21");  
  29.                 aaa.Add("score""1001");  
  30.                 aa.Add(aaa);  
  31.             }  
  32.             string json = JsonConvert.SerializeObject(aa, Formatting.Indented);  
  33.             context.Response.Write(json);  
  34.         }  
  35.         if (type == "3") //层级数据  
  36.         {  
  37.             List<Student> list = new List<Student>();  
  38.             for (int i = 0; i < 6; i++)  
  39.             {  
  40.                 Student a = new Student();  
  41.                 a.id = "no" + i;  
  42.                 a.name = "张三" + i;  
  43.                 a.age = "21";  
  44.                   
  45.                 Dictionary<stringstring> dic = new Dictionary<stringstring>();  
  46.                 dic.Add("语文","80");  
  47.                 dic.Add("数学""81");  
  48.                 dic.Add("英语""83");  
  49.                 dic.Add("生物""89");  
  50.                 dic.Add("化学""90");  
  51.                 dic.Add("物理""95");  
  52.                 a.score = dic;  
  53.                 list.Add(a);  
  54.             }  
  55.             string json = JsonConvert.SerializeObject(list, Formatting.Indented);  
  56.             context.Response.Write(json);  
  57.         }          
  58.     }  
  59.   
  60.     public struct Student  
  61.     {  
  62.         public string id;  
  63.         public string name;  
  64.         public string age;  
  65.         public Dictionary<string,string> score;  
  66.     }  
  67.   
  68.     public bool IsReusable  
  69.     {  
  70.         get  
  71.         {  
  72.             return false;  
  73.         }  
  74.     }  
  75. }