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