• C# 产生JSON串


    由于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;
                }
            }
    
         

    希望能找个更好点的。

  • 相关阅读:
    有上下界的可行流
    NOIP模拟——change
    NOIP模拟 ———number(假的数位dp)
    18.8.18NOIP模拟。。。。Snow
    字符串算法(KMP,Trie树,AC自动机)
    BZOJ4293 Siano
    NOIP2017 DAY2 T1
    AtCoder Grand Contest 023 D GO Home
    浅谈SPFA(队列优化的Bellman-Ford算法)
    最短路最基本算法———Floyd算法
  • 原文地址:https://www.cnblogs.com/cwmizlp/p/9973340.html
Copyright © 2020-2023  润新知