一佳一

记录像1+1一样简洁的代码

导航

根据城市表生成json数据

Posted on 2017-10-13 18:52  一佳一  阅读(646)  评论(0编辑  收藏  举报
         T_CityManager am = new T_CityManager();
                string tou = "var LAreaData=[";
                string value = "";
                List<T_City> plist = am.GetAllBySQL(" AND FParentID=0").ToList();
                int count = 0;
                foreach (T_City prov in plist)
                {
                    if (count == 0)
                    {
                        value += "{\"id\":\"" + prov.PID + "\",\"name\":\"" + prov.FName + "\"";

                    }
                    else
                    {
                        value += ",{\"id\":\"" + prov.PID + "\",\"name\":\"" + prov.FName + "\"";
                    }

                    List<T_City> slist = am.GetAllBySQL(" AND FParentID=" + prov.PID).ToList();
                    if (slist.Count > 0)
                    {
                        value += ",\"child\":[";
                    }
                    else if (slist.Count == 1)
                    {
                        value += "},";
                    }


                    foreach (T_City city in slist)
                    {

                        value += "{\"id\":\"" + city.PID + "\",\"name\":\"" + city.FName + "\"";
                        List<T_City> qlist = am.GetAllBySQL(" AND FParentID=" + city.PID).ToList();
                        if (qlist.Count==0)
                        {
                            value += "},";
                        }
                        if (qlist.Count > 0)
                        {
                            value += ",\"child\":[";
                        }

                        foreach (var qu in qlist)
                        {
                            value += "{\"id\":\"" + qu.PID + "\",\"name\":\"" + qu.FName + "\"},";
                        }
                        if (qlist.Count > 0)
                        {
                            value = value.Substring(0, value.Length - 1);
                            value += "]},";
                        }
                    }
                    if (slist.Count > 0)
                    {
                        value = value.Substring(0, value.Length - 1);
                        value += "]}";
                    }

                    count++;
                }
                tou = tou + value + "]";
            }