1. <dd id="erndk"></dd>
                1. C# 生成多層嵌套JSON到數組中(樹結構JSON)

                  互聯網 2022/4/30 22:12:40

                  1 DataTable dt = DbHelperSQL.Query(sql).Tables[0];2 var changeinfo = string.Empty;3 JavaScriptSerializer js = new JavaScriptSerializer();4 5 List<OrderListInfo> lstoutinfo = new List<OrderListInfo>();6 7 OrderListInfo outinfo = new OrderLi…
                   1 DataTable dt = DbHelperSQL.Query(sql).Tables[0];
                   2 var changeinfo = string.Empty;
                   3 JavaScriptSerializer js = new JavaScriptSerializer();
                   4 
                   5 List<OrderListInfo> lstoutinfo = new List<OrderListInfo>();
                   6 
                   7 OrderListInfo outinfo = new OrderListInfo();
                   8 
                   9 outinfo.message = 0;
                  10 
                  11 var orderno = string.Empty;
                  12 var noxiangdeng = false;
                  13 for (var i = 0; i < dt.Rows.Count; i++)
                  14 {
                  15     switch (Convert.ToInt32(dt.Rows[i]["goodshipstatus"]))
                  16     {
                  17         case 0:
                  18             changeinfo = "等待付款";
                  19             break;
                  20         case 1:
                  21             changeinfo = "訂單支付";
                  22             break;
                  23         case 2:
                  24             changeinfo = "訂單發貨";
                  25             break;
                  26         case 3:
                  27             changeinfo = "退貨/退款中訂單";
                  28             break;
                  29         case 4:
                  30             changeinfo = "訂單完成";
                  31             break;
                  32         case 5:
                  33             changeinfo = "訂單取消";
                  34             break;
                  35     }
                  36     //初始化是否重復狀態
                  37     noxiangdeng = false;
                  38     //不相等的訂單號執行
                  39     if (!dt.Rows[i]["orderno"].ToString().Equals(orderno))
                  40     {
                  41         outinfo = new OrderListInfo();
                  42         outinfo.result = true;
                  43         outinfo.ordertime = dt.Rows[i]["ordertime"].ToString();
                  44         outinfo.orderno = dt.Rows[i]["orderno"].ToString();
                  45         outinfo.orderprice = Convert.ToDecimal(dt.Rows[i]["orderprice"]);
                  46         outinfo.orderstatus = Convert.ToChar(dt.Rows[i]["orderstatus"]);
                  47         outinfo.expressname = dt.Rows[i]["expressname"].ToString();
                  48         outinfo.expresstel = dt.Rows[i]["expresstel"].ToString();
                  49         outinfo.expressaddress = dt.Rows[i]["expressaddress"].ToString();
                  50 
                  51     }
                  52     else
                  53     { //訂單號重復時只記錄明細信息
                  54         noxiangdeng = true;
                  55         outinfo.orderdetails.Add(new OrderDetails()
                  56         {
                  57             orderid = int.Parse(dt.Rows[i]["ordersid"].ToString()),
                  58             goodsid=int.Parse(dt.Rows[i]["goodsid"].ToString()),
                  59             goodsimg = dt.Rows[i]["goodsimg"].ToString(),
                  60             goodstitle = dt.Rows[i]["goodstitle"].ToString(),
                  61             goodsprice = Convert.ToDecimal(dt.Rows[i]["goodsprice"].ToString()),
                  62             buysumqty = int.Parse(dt.Rows[i]["buysumqty"].ToString()),
                  63             goodshipmode = dt.Rows[i]["goodshipmode"].ToString(),
                  64             goodshipstatus = changeinfo,
                  65             shopname = dt.Rows[i]["shopname"].ToString(),
                  66             shopphone = dt.Rows[i]["shopphone"].ToString()
                  67         }); 
                  68         continue;
                  69     }
                  70     outinfo.orderdetails = new List<OrderDetails>();
                  71     outinfo.orderdetails.Add(new OrderDetails()
                  72     {
                  73         orderid = int.Parse(dt.Rows[i]["ordersid"].ToString()),
                  74         goodsimg = dt.Rows[i]["goodsimg"].ToString(),
                  75         goodstitle = dt.Rows[i]["goodstitle"].ToString(),
                  76         goodsprice = Convert.ToDecimal(dt.Rows[i]["goodsprice"].ToString()),
                  77         buysumqty = int.Parse(dt.Rows[i]["buysumqty"].ToString()),
                  78         goodshipmode = dt.Rows[i]["goodshipmode"].ToString(),
                  79         goodshipstatus = changeinfo,
                  80         shopname = dt.Rows[i]["shopname"].ToString(),
                  81         shopphone = dt.Rows[i]["shopphone"].ToString()
                  82     });
                  83  
                  84     orderno = dt.Rows[i]["orderno"].ToString();
                  85     if (noxiangdeng != true)
                  86     {
                  87         lstoutinfo.Add(outinfo); 
                  88     }
                  89 }
                  90 
                  91  string json = js.Serialize(lstoutinfo);
                  92  Context.Response.Write(json);
                   1 #region 獲取用戶訂單列表及訂單明細用
                   2 public class OrderListInfo
                   3 {
                   4     public bool result { get; set; }
                   5     public string ordertime { get; set; }
                   6     public string orderno { get; set; }
                   7     public decimal orderprice { get; set; }
                   8     public char orderstatus { get; set; }
                   9     public List<OrderDetails> orderdetails { get; set; }
                  10 
                  11     public int message { get; set; }
                  12 
                  13     public string expressname { get; set; }
                  14     public string expressaddress { get; set; }
                  15     public string expresstel { get; set; }
                  16 }
                  17 public class OrderDetails
                  18 {
                  19     public int orderid { get; set; }
                  20     public int goodsid { get; set; }
                  21     public string goodsimg { get; set; }
                  22     public string goodstitle { get; set; }
                  23     public decimal goodsprice { get; set; }
                  24     public int buysumqty { get; set; }
                  25     public string goodshipmode { get; set; }
                  26     public string goodshipno { get; set; }
                  27     public string goodshipstatus { get; set; }
                  28     public string shopname { get; set; }
                  29     public string shopphone { get; set; }
                  30 
                  31     public string goodscode { get; set; }
                  32     public string goodscost { get; set; }
                  33     public string goodstock { get; set; }
                  34 }
                  35 //根據訂單號獲取用戶訂單信息
                  36 public class OrderDetailsInfo
                  37 {
                  38     public bool result { get; set; }
                  39     public string ordertime { get; set; }
                  40     public string orderno { get; set; }
                  41     public decimal orderprice { get; set; }
                  42     public char orderstatus { get; set; }
                  43 
                  44     public string expressname { get; set; }
                  45     public string expressaddress { get; set; }
                  46     public string expresstel { get; set; }
                  47 
                  48     public string ordersource { get; set; }
                  49     public string paymode { get; set; }
                  50     public string paymenttime { get; set; }
                  51     public string remark { get; set; }
                  52     
                  53     public List<OrderDetails> orderdetails { get; set; }
                  54 
                  55     public int message { get; set; }
                  56 }
                  57 #endregion

                  輸出JSON格式為:

                   1 [
                   2     {
                   3         "result": true,
                   4         "ordertime": "2016-08-17",
                   5         "orderno": "3333333333333",
                   6         "orderprice": 110,
                   7         "orderdetails": [
                   8             {
                   9                 "goodsimg": "UpLoadImg/GoodsImage/931be419-e9d3-4dae-ae93-5af619c217d9.jpg",
                  10                 "goodstitle": "山東特產 山東大棗1000g",
                  11                 "goodsprice": 99,
                  12                 "buysumqty": 1,
                  13                 "goodshipmode": "",
                  14                 "goodshipno": null,
                  15                 "goodshipstatus": "訂單發貨",
                  16                 "shopname": "兩顆牙",
                  17                 "shopphone": "834613646"
                  18             },
                  19             {
                  20                 "goodsimg": "UpLoadImg/GoodsImage/e4766b9f-21be-49f2-b432-312c5dda74da.jpg",
                  21                 "goodstitle": "周村燒餅",
                  22                 "goodsprice": 99,
                  23                 "buysumqty": 3,
                  24                 "goodshipmode": "",
                  25                 "goodshipno": null,
                  26                 "goodshipstatus": "訂單發貨",
                  27                 "shopname": "三只松鼠",
                  28                 "shopphone": "827878787"
                  29             }
                  30         ],
                  31         "message": 0
                  32     },
                  33     {
                  34         "result": true,
                  35         "ordertime": "2016-08-18",
                  36         "orderno": "2185944294790661",
                  37         "orderprice": 110,
                  38         "orderdetails": [
                  39             {
                  40                 "goodsimg": "UpLoadImg/GoodsImage/931be419-e9d3-4dae-ae93-5af619c217d9.jpg",
                  41                 "goodstitle": "山東特產 山東大棗1000g",
                  42                 "goodsprice": 99,
                  43                 "buysumqty": 3,
                  44                 "goodshipmode": "順豐速運",
                  45                 "goodshipno": null,
                  46                 "goodshipstatus": "訂單發貨",
                  47                 "shopname": "兩顆牙",
                  48                 "shopphone": "834613646"
                  49             },
                  50             {
                  51                 "goodsimg": "UpLoadImg/GoodsImage/e4766b9f-21be-49f2-b432-312c5dda74da.jpg",
                  52                 "goodstitle": "周村燒餅",
                  53                 "goodsprice": 99,
                  54                 "buysumqty": 1,
                  55                 "goodshipmode": "",
                  56                 "goodshipno": null,
                  57                 "goodshipstatus": "訂單發貨",
                  58                 "shopname": "三只松鼠",
                  59                 "shopphone": "827878787"
                  60             },
                  61             {
                  62                 "goodsimg": "UpLoadImg/GoodsImage/80fce99b-5308-4f7f-889e-e55a1a15c536.jpg",
                  63                 "goodstitle": "星月菩提108顆 包郵",
                  64                 "goodsprice": 99,
                  65                 "buysumqty": 1,
                  66                 "goodshipmode": "中通速遞",
                  67                 "goodshipno": null,
                  68                 "goodshipstatus": "訂單發貨",
                  69                 "shopname": "兩顆牙",
                  70                 "shopphone": "834613646"
                  71             }
                  72         ],
                  73         "message": 0
                  74     }
                  75 ]

                   

                  隨時隨地學軟件編程-關注百度小程序和微信小程序
                  關于找一找教程網

                  本站文章僅代表作者觀點,不代表本站立場,所有文章非營利性免費分享。
                  本站提供了軟件編程、網站開發技術、服務器運維、人工智能等等IT技術文章,希望廣大程序員努力學習,讓我們用科技改變世界。
                  [C# 生成多層嵌套JSON到數組中(樹結構JSON)]http://www.yachtsalesaustralia.com/tech/detail-318294.html

                  贊(0)
                  關注微信小程序
                  程序員編程王-隨時隨地學編程

                  掃描二維碼或查找【程序員編程王】

                  可以隨時隨地學編程啦!

                  技術文章導航 更多>
                  掃一掃關注最新編程教程
                  国产在线拍揄自揄视频菠萝

                        1. <dd id="erndk"></dd>