Article From:https://www.cnblogs.com/cwmizlp/p/9973340.html

 

Because JSON is the standard of front-end and back-end transmission, it is very important to produce JSON string quickly now.

After looking for so long, it’s easy to find one.

 

 

        ///<summary>
        ///Query provinces and municipalities/// </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; });
                //Collection of all provinces
               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;
                   //A collection of all municipalities
                   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;
                       //Collection of all zones
                       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
                   });

                   //Put Jilin Province in the First Place
                       if (item.Name == "Jilin Province")
                       {
                           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;
            }
        }

     

 

 

Hope to find a better one.

Link of this Article: C# Generates JSON Strings

Leave a Reply

Your email address will not be published. Required fields are marked *