2016-02-10 86 views
0

我需要序列化TreeView节点为JSON格式并反序列化回树视图。将Treeview序列化为JSON并反序列化回树视图

我有一个自定义的子节点类如下图所示

Class SubNode: TreeNode 
{ 
    dynamic obj; 
} 

所以创建一个树节点时,它也会有一个复杂的对象中的每个节点,如下图所示。

SubNode sub = new SubNode(); 
sub.obj.property = "Value1" 
sub.obj.Complex.Prooerty = "Value2" 

等等....

能否请你让我知道我们如何实现这一目标?感谢提前一百万!

回答

0

看看那里现有的图书馆,着名的图书馆使用JSON.NET。你可以看看如何做的例子。

序列化的对象

public class Account 
{ 
    public string Email { get; set; } 
    public bool Active { get; set; } 
    public DateTime CreatedDate { get; set; } 
    public IList<string> Roles { get; set; } 
} 

Account account = new Account 
{ 
    Email = "[email protected]", 
    Active = true, 
    CreatedDate = new DateTime(2013, 1, 20, 0, 0, 0, DateTimeKind.Utc), 
    Roles = new List<string> 
    { 
     "User", 
     "Admin" 
    } 
}; 

string json = JsonConvert.SerializeObject(account, Formatting.Indented); 
// { 
// "Email": "[email protected]", 
// "Active": true, 
// "CreatedDate": "2013-01-20T00:00:00Z", 
// "Roles": [ 
//  "User", 
//  "Admin" 
// ] 
// } 

Console.WriteLine(json); 

反序列化对象

string json = @"{ 
    'Email': '[email protected]', 
    'Active': true, 
    'CreatedDate': '2013-01-20T00:00:00Z', 
    'Roles': [ 
    'User', 
    'Admin' 
    ] 
}"; 

Account account = JsonConvert.DeserializeObject<Account>(json); 

Console.WriteLine(account.Email);