三层JSON数组C#方法

Posted 最爱吃汤圆27

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了三层JSON数组C#方法相关的知识,希望对你有一定的参考价值。

 /// <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#方法的主要内容,如果未能解决你的问题,请参考以下文章

json三层解析(数组解析)

c#和php接收json数据方法

Alamofire 文件上传出现错误“JSON 文本未以数组或对象开头,并且允许未设置片段的选项”

C#常用代码片段备忘

VSCode自定义代码片段—— 数组的响应式方法

VSCode自定义代码片段10—— 数组的响应式方法