zoukankan      html  css  js  c++  java
  • C#嵌套类

    {
    	"children" : [{
    			"children" : [{
    					"children" : [],
    					"name" : "b"
    				}
    			],
    			"name" : "aaa"
    		}, {
    			"children" : [{
    					"children" : [],
    					"name" : "t22Name"
    				}, {
    					"children" : [],
    					"name" : "b"
    				}
    			],
    			"name" : "t2"
    		}
    	],
    	"name" : "a"
    }

    上面的JSON要使用C#生成,自己拼字符串很麻烦,需要一个嵌套类,类很简单:

        class nestedClass
        {
            public string name { get; set; }
            public List<nestedClass> children = new List<nestedClass>();
        }


    使用示例:

    nestedClass t = new nestedClass();
    t.name = "a";
    nestedClass t1 = new nestedClass();
    t1.name = "aaa";
    nestedClass t11 = new nestedClass();
    t11.name = "b";
    t1.children.Add(t11);
    t.children.Add(t1);
    nestedClass t2 = new nestedClass();
    t2.name = "t2";
    t.children.Add(t2);
    
    nestedClass t22 = new nestedClass();
    t22.name = "t22Name";
    t2.children.Add(t22);
    t2.children.Add(t11);
    JSONHelper.WriteJSON(t);
    



  • 相关阅读:
    Python 基础
    Python 基础
    Python 基础
    Python 基础
    Python 基础
    Python 基础
    Python 基础
    Python 基础
    Python 基础
    Python 基础
  • 原文地址:https://www.cnblogs.com/apollokk/p/6713806.html
Copyright © 2011-2022 走看看