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

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/kongwei521/article/details/52522632
                DataTable dt = DbHelperSQL.Query(sql).Tables[0];
                    var changeinfo = string.Empty;
                    JavaScriptSerializer js = new JavaScriptSerializer();

                    List<OrderListInfo> lstoutinfo = new List<OrderListInfo>();

                    OrderListInfo outinfo = new OrderListInfo();
                 
                    outinfo.message = 0;
                 
                    var orderno = string.Empty;
                    var noxiangdeng = false;
                    for (var i = 0; i < dt.Rows.Count; i++)
                    {
                        switch (Convert.ToInt32(dt.Rows[i]["goodshipstatus"]))
                        {
                            case 0:
                                changeinfo = "等待付款";
                                break;
                            case 1:
                                changeinfo = "订单支付";
                                break;
                            case 2:
                                changeinfo = "订单发货";
                                break;
                            case 3:
                                changeinfo = "退货/退款中订单";
                                break;
                            case 4:
                                changeinfo = "订单完成";
                                break;
                            case 5:
                                changeinfo = "订单取消";
                                break;
                        }
                        //初始化是否重复状态
                        noxiangdeng = false;
                        //不相等的订单号执行
                        if (!dt.Rows[i]["orderno"].ToString().Equals(orderno))
                        {
                            outinfo = new OrderListInfo();
                            outinfo.result = true;
                            outinfo.ordertime = dt.Rows[i]["ordertime"].ToString();
                            outinfo.orderno = dt.Rows[i]["orderno"].ToString();
                            outinfo.orderprice = Convert.ToDecimal(dt.Rows[i]["orderprice"]);
                            outinfo.orderstatus = Convert.ToChar(dt.Rows[i]["orderstatus"]);
                            outinfo.expressname = dt.Rows[i]["expressname"].ToString();
                            outinfo.expresstel = dt.Rows[i]["expresstel"].ToString();
                            outinfo.expressaddress = dt.Rows[i]["expressaddress"].ToString();

                        }
                        else
                        { //订单号重复时只记录明细信息
                            noxiangdeng = true;
                            outinfo.orderdetails.Add(new OrderDetails()
                            {
                                orderid = int.Parse(dt.Rows[i]["ordersid"].ToString()),
                                goodsid=int.Parse(dt.Rows[i]["goodsid"].ToString()),
                                goodsimg = dt.Rows[i]["goodsimg"].ToString(),
                                goodstitle = dt.Rows[i]["goodstitle"].ToString(),
                                goodsprice = Convert.ToDecimal(dt.Rows[i]["goodsprice"].ToString()),
                                buysumqty = int.Parse(dt.Rows[i]["buysumqty"].ToString()),
                                goodshipmode = dt.Rows[i]["goodshipmode"].ToString(),
                                goodshipstatus = changeinfo,
                                shopname = dt.Rows[i]["shopname"].ToString(),
                                shopphone = dt.Rows[i]["shopphone"].ToString()
                            }); 
                            continue;
                        }
                        outinfo.orderdetails = new List<OrderDetails>();
                        outinfo.orderdetails.Add(new OrderDetails()
                        {
                            orderid = int.Parse(dt.Rows[i]["ordersid"].ToString()),
                            goodsimg = dt.Rows[i]["goodsimg"].ToString(),
                            goodstitle = dt.Rows[i]["goodstitle"].ToString(),
                            goodsprice = Convert.ToDecimal(dt.Rows[i]["goodsprice"].ToString()),
                            buysumqty = int.Parse(dt.Rows[i]["buysumqty"].ToString()),
                            goodshipmode = dt.Rows[i]["goodshipmode"].ToString(),
                            goodshipstatus = changeinfo,
                            shopname = dt.Rows[i]["shopname"].ToString(),
                            shopphone = dt.Rows[i]["shopphone"].ToString()
                        });
                     
                        orderno = dt.Rows[i]["orderno"].ToString();
                        if (noxiangdeng != true)
                        {
                            lstoutinfo.Add(outinfo); 
                        }
                    }

                     string json = js.Serialize(lstoutinfo);
                     Context.Response.Write(json);


	#region 获取用户订单列表及订单明细用
		public class OrderListInfo
		{
			public bool result { get; set; }
			public string ordertime { get; set; }
			public string orderno { get; set; }
			public decimal orderprice { get; set; }
			public char orderstatus { get; set; }
			public List<OrderDetails> orderdetails { get; set; }

			public int message { get; set; }

			public string expressname { get; set; }
			public string expressaddress { get; set; }
			public string expresstel { get; set; }
		}
		public class OrderDetails
		{
			public int orderid { get; set; }
			public int goodsid { get; set; }
			public string goodsimg { get; set; }
			public string goodstitle { get; set; }
			public decimal goodsprice { get; set; }
			public int buysumqty { get; set; }
			public string goodshipmode { get; set; }
			public string goodshipno { get; set; }
			public string goodshipstatus { get; set; }
			public string shopname { get; set; }
			public string shopphone { get; set; }

			public string goodscode { get; set; }
			public string goodscost { get; set; }
			public string goodstock { get; set; }
		}
		//根据订单号获取用户订单信息
		public class OrderDetailsInfo
		{
			public bool result { get; set; }
			public string ordertime { get; set; }
			public string orderno { get; set; }
			public decimal orderprice { get; set; }
			public char orderstatus { get; set; }

			public string expressname { get; set; }
			public string expressaddress { get; set; }
			public string expresstel { get; set; }
		
			public string ordersource { get; set; }
			public string paymode { get; set; }
			public string paymenttime { get; set; }
			public string remark { get; set; }
			
			public List<OrderDetails> orderdetails { get; set; }

			public int message { get; set; }
		}
		#endregion

输出JSON格式为:
[
    {
        "result": true,
        "ordertime": "2016-08-17",
        "orderno": "3333333333333",
        "orderprice": 110,
        "orderdetails": [
            {
                "goodsimg": "UpLoadImg/GoodsImage/931be419-e9d3-4dae-ae93-5af619c217d9.jpg",
                "goodstitle": "山东特产 山东大枣1000g",
                "goodsprice": 99,
                "buysumqty": 1,
                "goodshipmode": "",
                "goodshipno": null,
                "goodshipstatus": "订单发货",
                "shopname": "两颗牙",
                "shopphone": "834613646"
            },
            {
                "goodsimg": "UpLoadImg/GoodsImage/e4766b9f-21be-49f2-b432-312c5dda74da.jpg",
                "goodstitle": "周村烧饼",
                "goodsprice": 99,
                "buysumqty": 3,
                "goodshipmode": "",
                "goodshipno": null,
                "goodshipstatus": "订单发货",
                "shopname": "三只松鼠",
                "shopphone": "827878787"
            }
        ],
        "message": 0
    },
    {
        "result": true,
        "ordertime": "2016-08-18",
        "orderno": "2185944294790661",
        "orderprice": 110,
        "orderdetails": [
            {
                "goodsimg": "UpLoadImg/GoodsImage/931be419-e9d3-4dae-ae93-5af619c217d9.jpg",
                "goodstitle": "山东特产 山东大枣1000g",
                "goodsprice": 99,
                "buysumqty": 3,
                "goodshipmode": "顺丰速运",
                "goodshipno": null,
                "goodshipstatus": "订单发货",
                "shopname": "两颗牙",
                "shopphone": "834613646"
            },
            {
                "goodsimg": "UpLoadImg/GoodsImage/e4766b9f-21be-49f2-b432-312c5dda74da.jpg",
                "goodstitle": "周村烧饼",
                "goodsprice": 99,
                "buysumqty": 1,
                "goodshipmode": "",
                "goodshipno": null,
                "goodshipstatus": "订单发货",
                "shopname": "三只松鼠",
                "shopphone": "827878787"
            },
            {
                "goodsimg": "UpLoadImg/GoodsImage/80fce99b-5308-4f7f-889e-e55a1a15c536.jpg",
                "goodstitle": "星月菩提108颗 包邮",
                "goodsprice": 99,
                "buysumqty": 1,
                "goodshipmode": "中通速递",
                "goodshipno": null,
                "goodshipstatus": "订单发货",
                "shopname": "两颗牙",
                "shopphone": "834613646"
            }
        ],
        "message": 0
    }
]


参考资料:

http://blog.163.com/hy_85/blog/static/1484587662011465235773/

http://www.jb51.net/article/48027.htm

http://www.cnblogs.com/chendaoyin/p/3601006.html


展开阅读全文

没有更多推荐了,返回首页