Ajax多重List解析Json

前端之家收集整理的这篇文章主要介绍了Ajax多重List解析Json前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

tips:使用前先引用jQuery


AjaxService.ashx — for循环DataTable组合成Json

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Web;
  7. using Unisoft.ICM.Utility.Data;
  8. using Unisoft.ICM.Utility.Web;
  9.  
  10. namespace ICM.Web.Home.AjaxService
  11. {
  12. /// <summary>
  13. /// 功能首页头部商品分类Json
  14. /// 作者:李志伟
  15. /// 时间:2016.03.22
  16. /// SelIndustryProductTypeData 的摘要说明
  17. /// </summary>
  18. public class SelIndustryProductTypeData : IHttpHandler//,System.Web.SessionState.IRequiresSessionState
  19. {
  20. public void ProcessRequest(HttpContext context)
  21. {
  22. context.Response.ContentType = "text/plain";
  23.  
  24. string IndustryID = context.Request["industryid"];
  25. string CompanyID = context.Request["companyid"];
  26.  
  27. string notesql = string.Format(@"select ProductTypeID,ProductTypeName from vw_Mall_Industry_ProductType where CompanyID={0} and IndustryID={1}",CompanyID,IndustryID);
  28. DataTable noteData = DbHelpersql.Query(notesql).Tables[0];
  29.  
  30. string artsql = string.Format(@"select TypeID,TypeName,ParentID from tbl_Stock_ProductType where ParentID in(select distinct ProductTypeID from vw_Mall_Industry_ProductType where CompanyID={0} and IndustryID={1})",IndustryID);
  31. DataTable artData = DbHelpersql.Query(artsql).Tables[0];
  32.  
  33. StringBuilder strJson = new StringBuilder();
  34.  
  35. strJson.Append("[");
  36. for (int i = 0; i < noteData.Rows.Count; i++)
  37. {
  38. strJson.Append("{");
  39. strJson.AppendFormat("\"ProductTypeID\":\"{0}\",",noteData.Rows[i]["ProductTypeID"].ToString());
  40. strJson.AppendFormat("\"ProductTypeName\":\"{0}\",noteData.Rows[i]["ProductTypeName"].ToString());
  41. strJson.Append("\"ProData\":");
  42. strJson.Append("[");
  43. DataRow[] dtRow = artData.Select(" ParentID=" + noteData.Rows[i]["ProductTypeID"].ToString());
  44. for (int k = 0; k < dtRow.Count(); k++)
  45. {
  46. strJson.Append("{");
  47. strJson.AppendFormat("\"TypeID\":\"{0}\",dtRow[k]["TypeID"].ToString());
  48. strJson.AppendFormat("\"TypeName\":\"{0}\"",dtRow[k]["TypeName"].ToString());
  49.  
  50. if (k != dtRow.Count()-1)
  51. strJson.Append("},");
  52. else
  53. strJson.Append("}");
  54. }
  55. strJson.Append("]");
  56. if (i != noteData.Rows.Count - 1)
  57. strJson.Append("},");
  58. else
  59. strJson.Append("}");
  60. }
  61. strJson.Append("]");
  62.  
  63. context.Response.Write(strJson.ToString());
  64. }
  65.  
  66. public bool IsReusable
  67. {
  68. get
  69. {
  70. return false;
  71. }
  72. }
  73.  
  74. /*
  75. 结果格式如下:
  76. [
  77. {
  78. "ProductTypeID":"1","ProductTypeName":"时尚童鞋","ProData":[
  79. {
  80. "TypeID":"1","TypeName":"轮滑鞋"
  81. },{
  82. "TypeID":"2","TypeName":"滑雪鞋"
  83. },{
  84. "TypeID":"3","TypeName":"足球鞋"
  85. }]
  86. },{
  87. "ProductTypeID":"1","ProductTypeName":"时尚男鞋","TypeName":"皮鞋"
  88. },"TypeName":"休闲鞋"
  89. },"TypeName":"跑步鞋"
  90. }]
  91. }
  92. //......................................
  93. ]
  94. */
  95. }
  96. }
  97.  






HTML — Ajax解析Json,for遍历内容

  1. function SelIndustryProductTypeData(IndustryID,CompanyID) {
  2. $.ajax({
  3. url: '/Home/AjaxService/SelIndustryProductTypeData.ashx',type: 'GET',data: { industryid: IndustryID,companyid: CompanyID },success: function (data) {
  4. var dt = eval("(" + data + ")");
  5. var item = "";
  6.  
  7. for (var k = 0; k <= dt.length - 1; k++) {
  8. item += "<dt class=\"mt15\">";
  9. item += "<p class=\"font14 bold\">" + dt[k].ProductTypeName + "</p>";
  10. item += "<span><img src=\"/images/index_iocn3_03.jpg\" width=\"12\" height=\"12\" /></span>";
  11. item += "</dt>";
  12. item += "<dd>";
  13.  
  14. var row = dt[k].ProData.length;
  15. for (var j = 0; j < row; j++) {
  16. item += "<a href=\"#\">" + dt[k].ProData[j].TypeName + "</a>";
  17. }
  18. item += "</dd>";
  19. }
  20. $("#nav-top").html(item);
  21. }
  22. });
  23. }

猜你在找的Ajax相关文章