十年河东,十年河西,莫欺骚年穷...打错一个字哈、~_~
接着上一篇博客,上一篇博客是=使用数据库结合LINQ构造的,为了方便理解,本篇采用泛型分组进行构造。
有兴趣的小虎斑可以参考上一篇博客:如何构造分层次的 Json 数据
本篇很简单,直接上代码
using Newtonsoft.Json; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace DFS { class Program { static void Main(string[] args) { ListMList = new List () { new HelloChinaModel(){ Id=1,Province="河南省",City="商丘市",District="永城市",OrgName="薛湖镇",AddTime=DateTime.Now}, new HelloChinaModel(){ Id=1,Province="河南省",City="商丘市",District="夏邑县",OrgName="火店乡",AddTime=DateTime.Now}, new HelloChinaModel(){ Id=1,Province="河南省",City="商丘市",District="睢阳区",OrgName="龙华小区",AddTime=DateTime.Now}, new HelloChinaModel(){ Id=1,Province="河南省",City="平顶山市",District="新华区",OrgName="河南城建学院",AddTime=DateTime.Now}, new HelloChinaModel(){ Id=1,Province="河南省",City="平顶山市",District="鲁山县",OrgName="炉山镇",AddTime=DateTime.Now}, new HelloChinaModel(){ Id=1,Province="河南省",City="洛阳市",District="阳逻县",OrgName="大杨镇",AddTime=DateTime.Now}, new HelloChinaModel(){ Id=1,Province="江苏省",City="苏州市",District="张家港市",OrgName="乐余镇",AddTime=DateTime.Now}, new HelloChinaModel(){ Id=1,Province="江苏省",City="苏州市",District="昆山市",OrgName="小上海镇",AddTime=DateTime.Now}, new HelloChinaModel(){ Id=1,Province="江苏省",City="南京市",District="雨花台区",OrgName="雨花台小区",AddTime=DateTime.Now}, new HelloChinaModel(){ Id=1,Province="江苏省",City="徐州市",District="沛县",OrgName="王文学镇",AddTime=DateTime.Now}, new HelloChinaModel(){ Id=1,Province="山东省",City="日照市",District="花果山县",OrgName="猴子镇",AddTime=DateTime.Now}, new HelloChinaModel(){ Id=1,Province="山东省",City="青岛市",District="青岛啤酒县",OrgName="扎啤镇",AddTime=DateTime.Now}, }; // 首先根据省份分组 var P_ls = MList.GroupBy(a => a.Province ).Select(g => (new { Province = g.Key, Citys = g })).ToList();//省份列表 // List PList = new List (); foreach (var item in P_ls) { Model_P P = new Model_P(); // // List CList = new List (); //获取各个分组 var Province = item.Province;//省份的名称 var CitysList = item.Citys;//省份对应的市 列表 var C_ls = CitysList.GroupBy(a => a.City ).Select(g => new { City = g.Key, Districts = g }) ;//市/区列表 foreach (var Citem in C_ls) { List DList = new List (); Model_C C = new Model_C(); // var DistrictsLsit = Citem.Districts;// var D_ls = DistrictsLsit.GroupBy(a => a.District ).Select(g => new { Districts = g.Key, OrgNames = g });//县/区列表 foreach (var Ditem in D_ls) { List OList = new List (); Model_D D = new Model_D(); var OrgNamesList = Ditem.OrgNames.ToList(); var O_ls = OrgNamesList.GroupBy(a =>a.OrgName ).Select(g => new { OrgName = g.Key });//乡镇列表/没有下级了 foreach (var Oitem in O_ls) { Model_O O = new Model_O(); string orgName = Oitem.OrgName.ToString(); O.OrgName = orgName; OList.Add(O); D.nodes = OList; } string Dname = Ditem.Districts.ToString(); D.District = Dname; DList.Add(D); C.nodes = DList; } string Cname = Citem.City.ToString(); C.City=Cname; CList.Add(C); P.nodes = CList; } P.Province = item.Province.ToString(); PList.Add(P); } string Json = JsonConvert.SerializeObject(PList); Console.WriteLine(Json); Console.ReadKey(); } } public partial class HelloChinaModel { public int Id { get; set; } public string Province { get; set; } public string City { get; set; } public string District { get; set; } public string OrgName { get; set; } public Nullable AddTime { get; set; } } /// /// 省 /// public class Model_P { public string Province { get; set; } public List nodes { get; set; } } /// /// 市/区 /// public class Model_C { public string City { get; set; } public List nodes { get; set; } } /// /// 县(县级市)/区 /// public class Model_D { public string District { get; set; } public List nodes { get; set; } } /// /// 镇/乡/其他 /// public class Model_O { public string OrgName { get; set; } } }
构造的数据如下:
[ { "Province": "河南省", "nodes": [ { "City": "商丘市", "nodes": [ { "District": "永城市", "nodes": [ { "OrgName": "薛湖镇" } ] }, { "District": "夏邑县", "nodes": [ { "OrgName": "火店乡" } ] }, { "District": "睢阳区", "nodes": [ { "OrgName": "龙华小区" } ] } ] }, { "City": "平顶山市", "nodes": [ { "District": "新华区", "nodes": [ { "OrgName": "河南城建学院" } ] }, { "District": "鲁山县", "nodes": [ { "OrgName": "炉山镇" } ] } ] }, { "City": "洛阳市", "nodes": [ { "District": "阳逻县", "nodes": [ { "OrgName": "大杨镇" } ] } ] } ] }, { "Province": "江苏省", "nodes": [ { "City": "苏州市", "nodes": [ { "District": "张家港市", "nodes": [ { "OrgName": "乐余镇" } ] }, { "District": "昆山市", "nodes": [ { "OrgName": "小上海镇" } ] } ] }, { "City": "南京市", "nodes": [ { "District": "雨花台区", "nodes": [ { "OrgName": "雨花台小区" } ] } ] }, { "City": "徐州市", "nodes": [ { "District": "沛县", "nodes": [ { "OrgName": "王文学镇" } ] } ] } ] }, { "Province": "山东省", "nodes": [ { "City": "日照市", "nodes": [ { "District": "花果山县", "nodes": [ { "OrgName": "猴子镇" } ] } ] }, { "City": "青岛市", "nodes": [ { "District": "青岛啤酒县", "nodes": [ { "OrgName": "扎啤镇" } ] } ] } ] } ]
@陈卧龙的博客