码迷,mamicode.com
首页 > 编程语言 > 详细

三层JSON数组C#方法

时间:2020-02-28 13:48:04      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:orm   数据   datatable   query   sonar   meta   returns   lld   成功   

 /// <summary>  
        /// dataTable转换成Json格式 JSON对应关系 
        /// </summary>  
        /// <param name="dt">需要转换AbNormallData的表格</param>  
        /// <returns></returns>  
        public static string Table2JsonAryAbNormallData(DataTable phenonmenondt, string station)
        {
            string json = string.Empty;
            if (phenonmenondt.Rows.Count <= 0)
            {
                return "{\"total\":" + 0 + ",\"meta\":{\"status\":201,\"msg\":\"无数据\"}}";
            }
            List<Phenomenon_Model> phelist = new List<Phenomenon_Model>();
            for (int i = 0; i < phenonmenondt.Rows.Count; i++)
            {
                List<Reason1_Model> reason1list = new List<Reason1_Model>();
                Phenomenon_Model phe = new Phenomenon_Model();
                phe.id = i;
                phe.value3 = phenonmenondt.Rows[i][0].ToString();
                DataTable reason1dt = SqlHelper.Query(sql语句);
                for (int j = 0; j < reason1dt.Rows.Count; j++)
                {
                    List<Reason2_Model> reason2list = new List<Reason2_Model>();
                    Reason1_Model rea1 = new Reason1_Model();
                    rea1.id = j;
                    rea1.value4 = reason1dt.Rows[j][0].ToString();
                    DataTable reason2dt = SqlHelper.Query(sql语句);
                    for (int n = 0; n < reason2dt.Rows.Count; n++)
                    {
                        Reason2_Model rea2 = new Reason2_Model();
                        rea2.id = n;
                        rea2.value5 = reason2dt.Rows[n][0].ToString();
                        reason2list.Add(rea2);
                    }
                    rea1.Children = reason2list;
                    reason1list.Add(rea1);
                }
                phe.Children = reason1list;
                phelist.Add(phe);
            }

            json = Newtonsoft.Json.JsonConvert.SerializeObject(phelist);//序列化对象
            json = "{" + "\"data\":" + json + "," + "\"meta\": {\"msg\": \"获取成功\",\"status\": 200}" + "}";
            return json;
        }
 public class Phenomenon_Model
    {
        /// <summary>
        /// ID编号
        /// </summary>
        public int id { get; set; }
        /// <summary>
        /// 现象
        /// </summary>
        public string value3 { get; set; }
        /// <summary>
        /// 子节点
        /// </summary>
        public List<Reason1_Model> Children { get; set; }
    }
 public class Reason1_Model
    {
        /// <summary>
        /// 编号ID
        /// </summary>
        public int id { get; set; }
        /// <summary>
        /// 原因1
        /// </summary>
        public string value4 { get; set; }
        /// <summary>
        /// 子节点
        /// </summary>
        public List<Reason2_Model> Children { get; set; }
    }
 public class Reason2_Model
    {
        /// <summary>
        /// ID
        /// </summary>
        public int id { get; set; }
        /// <summary>
        /// Reason2
        /// </summary>
        public string value5 { get; set; }
    }

三层JSON对应的Model

三层JSON数组C#方法

标签:orm   数据   datatable   query   sonar   meta   returns   lld   成功   

原文地址:https://www.cnblogs.com/cdjbolg/p/12376782.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!