zoukankan      html  css  js  c++  java
  • 递归循环JSON

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    /// <summary>
    /// JsonHelper 的摘要说明
    /// </summary>
    public class JsonHelper
    {
        /// <summary>
        /// 转换JSON对象
        /// </summary>
        /// <param name="company"></param>
        /// <returns></returns>
        public static string ConvertToJson(Company company)
        {
            string json = "CompanyName:"" + company.CompanyName + "",ContactName:"" + company.ContactName + "",City:"" + company.City + "",CustomerID:"" + company.CustomerID + "",children:{0}";
            return json;
        }
    
        /// <summary>
        /// 转换JSON对象集合,包含子集,递归加载
        /// </summary>
        /// <param name="companyList"></param>
        /// <returns></returns>
        public static string ConvertToJson(List<Company> companyList)
        {
            string json = "[";
            //获取第一级目录
            List<Company> rootList = companyList.Where(x => string.IsNullOrEmpty(x.Pid)).ToList<Company>();
            foreach (Company root in rootList)
            {
                string js = ConvertToJson(root);
                string children="";
                children = DiGui(companyList, children, root.CustomerID);
                json += "{"+string.Format(js, children) + "},";
            }
            if (json.LastIndexOf(",") < 1)
            {
                json += "]";
            }
            else
            {
                json = json.Substring(0, json.Length - 1) + "]";
            }
            return json.Replace(",children:[]", "");
        }
    
        /// <summary>
        /// 递归调用添加包含子集的JSON数组
        /// </summary>
        private static string DiGui(List<Company> companyList,string children,string pid)
        {
            children = "[";
            List<Company> childerList = companyList.Where(x => x.Pid.ToUpper() == pid.ToUpper()).ToList<Company>();
            foreach (Company item in childerList)
            {
                string js = ConvertToJson(item);
                string cd = "";
                cd = DiGui(companyList, cd, item.CustomerID);
                children += "{"+string.Format(js, cd) + "},";
            }
            if (children.LastIndexOf(",") < 1)
            {
                children += "]";
            }
            else
            {
                children = children.Substring(0, children.Length - 1) + "]";
            }
            return children;
        }
    
        
    
    }


  • 相关阅读:
    记录使用cx_Freeze打包Python成exe可执行程序
    jquery plug-in DataTable API中文文档参考
    java.lang.UnsatisfiedLinkError: dlopen failed: /data/app/xxx/lib/arm/liblame.so: has text relocations
    CocoaPods的安装及使用
    Android 贝塞尔曲线的浅析
    GUI学习中错误Exception in thread "main" java.lang.NullPointerException
    线程
    12月13日
    今天开始学习java编程
    UVA10140 Prime Distance
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3369568.html
Copyright © 2011-2022 走看看