编程语言
首页 > 编程语言> > C# 生成多层嵌套JSON到数组中(树结构JSON)

C# 生成多层嵌套JSON到数组中(树结构JSON)

作者:互联网

 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 ]

 

标签:set,string,get,C#,树结构,JSON,Rows,dt,public
来源: https://www.cnblogs.com/lgx5/p/16211278.html