由于JSON是现在前后台传输的标准,快速生产JSON串现在很是重要啊,
找了那么久,找到一个还算简单的。
///<summary> ///查询省市区 /// </summary> public string SearchArea() { string Data = HttpRuntime.Cache["AreaName"] as string; if (Data == null) { IList<Mobile.Domain.Entities.Area> AreaList = AreaRepository.LoadByCondition("From Area", new string[] { }, new object[] { }); var AreaListFirst = AreaList.Where((name) => { return name.Parent_ID == 0; }); //所有省的集合 List<JObject> JAreaListFirst = new List<JObject>(); foreach (var item in AreaListFirst) { var Second = from r in AreaList where r.Parent_ID == item.ID select r; //所有市的集合 List<JObject> JAreaListSecond = new List<JObject>(); ; foreach (var item2 in Second) { var Three = from r in AreaList where r.Parent_ID == item2.ID select r; //所有区的集合 List<JObject> JAreaListThree = new List<JObject>(); ; foreach (var item3 in Three) { JObject obj1 = JObject.FromObject(new { id = item3.ID.ToString(), value =item3.Name }); JAreaListThree.Add(obj1); } JObject obj2 = JObject.FromObject(new { id = item2.ID.ToString(), value = item2.Name, childs = JAreaListThree }); JAreaListSecond.Add(obj2); } JObject obj3 = JObject.FromObject(new { id = item.ID.ToString(), value = item.Name, childs = JAreaListSecond }); //将吉林省放到第一位 if (item.Name == "吉林省") { JAreaListFirst.Insert(0, obj3); } else { JAreaListFirst.Add(obj3); } } string PP = Newtonsoft.Json.JsonConvert.SerializeObject(JAreaListFirst, Newtonsoft.Json.Formatting.Indented); HttpRuntime.Cache.Add("AreaName", PP, null, DateTime.MaxValue, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null); return PP; } else { return Data; } }
希望能找个更好点的。