private static List <CarItem> GetJson2ItemPri(JContainer json) { List <CarItem> list = new List <CarItem>(); //Newtonsoft.Json.Linq.JContainer json = (Newtonsoft.Json.Linq.JContainer)Newtonsoft.Json.JsonConvert.DeserializeObject(JsonData); for (int i = 0; i < json.Count; i++) { JObject jo = (JObject)json[i]; JToken jk = jo["List"]; CarItem ci = new CarItem() { UrlI = jo["I"].ToString(), N = jo["N"].ToString(), L = jo["L"] == null ? "" : jo["L"].ToString() }; ci.List = new List <CarItem>(); int index = jk == null ? 0 : jk.Count(); if (index > 0) { ci.N += string.Format("({0})", index); ci.List.AddRange(GetJson2ItemPri((JContainer)jk));//递归List中的数据 } list.Add(ci); #region 完善的结果 //foreach (JObject item in jk) //{ // JToken subjk = item["List"]; // CarItem subci = new CarItem() // { // UrlI = item["I"].ToString(), // N = item["N"].ToString(), // L = item["L"] == null ? "" : item["L"].ToString() // }; // subci.List = new List<CarItem>(); // foreach (JObject subitem in subjk) // { // JToken subjk1 = subitem["List"]; // CarItem subci1 = new CarItem() // { // UrlI = subitem["I"].ToString(), // N = subitem["N"].ToString(), // L = item["L"] == null ? "" : item["L"].ToString() // }; // subci.List.Add(subci1); // } // ci.List.Add(subci); //} //list.Add(ci); #endregion } return(list); }
private static TreeNode GetJson2NodePri(JContainer json) { TreeNode tn = new TreeNode(); for (int i = 0; i < json.Count; i++) { TreeNode td = new TreeNode(); JObject jo = (JObject)json[i]; JToken jk = jo["List"]; CarItem ci = new CarItem() { UrlI = jo["I"].ToString(), N = jo["N"].ToString(), L = jo["L"] == null ? "" : jo["L"].ToString() }; ci.List = new List <CarItem>(); int index = jk == null ? 0 : jk.Count(); if (index > 0) { ci.List.AddRange(GetJson2ItemPri((JContainer)jk)); td = GetJson2NodePri((JContainer)jk); } td.Name = ci.N; td.Text = ci.DisplayName; td.Tag = ci; if (!string.IsNullOrEmpty(ci.L)) { var treens = tn.Nodes.Find(ci.L, false); if (treens.Length > 0) { //找到根 TreeNode tnd = treens[0]; Font subfont = new Font("微软雅黑", 9F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134))); td.NodeFont = subfont; td.ForeColor = Color.Black; tnd.Nodes.Add(td); } else { //没有找到就创建 TreeNode tnd = new TreeNode(ci.L); tnd.Name = ci.L; Font subfont = new Font("微软雅黑", 9F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134))); td.NodeFont = subfont; td.ForeColor = Color.Black; tnd.Nodes.Add(td); tnd.Expand(); tn.Nodes.Add(tnd); } } else { Font subfont = new Font("微软雅黑", 9F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134))); td.NodeFont = subfont; td.ForeColor = Color.BlueViolet; tn.Nodes.Add(td); } } return(tn); }