2014-09-24 84 views
2

我有一個JSON文件夾結構,需要反序列化爲一個c#對象。我想知道如何在不設置多個子類對象的情況下做到這一點(因爲可能有大量的子文件夾)。我在想也許一個繼承父對象的子對象,或者有一個包含它自己的對象可能是要走的路,但我很難過!將JSON文件夾結構反序列化爲C#對象

乾杯!

JSON結構:

[ 
    { 
    type: "folder", 
    name: "animals", 
    path: "/animals", 
    children: [ 
     { 
     type: "folder", 
     name: "cat", 
     path: "/animals/cat", 
     children: [ 
      { 
      type: "folder", 
      name: "images", 
      path: "/animals/cat/images", 
      children: [ 
       { 
       type: "file", 
       name: "cat001.jpg", 
       path: "/animals/cat/images/cat001.jpg" 
       }, { 
       type: "file", 
       name: "cat001.jpg", 
       path: "/animals/cat/images/cat002.jpg" 
       } 
      ] 
      } 
     ] 
     } 
    ] 
    } 
] 

Json2CSharp輸出:

public class Child3 
{ 
    public string type { get; set; } 
    public string name { get; set; } 
    public string path { get; set; } 
} 

public class Child2 
{ 
    public string type { get; set; } 
    public string name { get; set; } 
    public string path { get; set; } 
    public List<Child3> children { get; set; } 
} 

public class Child 
{ 
    public string type { get; set; } 
    public string name { get; set; } 
    public string path { get; set; } 
    public List<Child2> children { get; set; } 
} 

public class RootObject 
{ 
    public string type { get; set; } 
    public string name { get; set; } 
    public string path { get; set; } 
    public List<Child> children { get; set; } 
} 

回答

1

只要結構是一樣的一路下跌,你應該只需要像:

public class Node { 
    public string type {get;set;} 
    public string name {get;set;} 
    public string path {get;set;} 
    public List<Node> children {get;set;} 
} 

依靠大多數序列化程序將完全忽略列表的事實,如果它是null

例如,通過Jil

List<Node> nodes = Jil.JSON.Deserialize<List<Node>>(json); 

和連載:

var obj = new List<Node> 
{ 
    new Node 
    { 
     type = "folder", 
     name = "animals", 
     path = "/animals", 
     children = new List<Node> 
     { 
      new Node 
      { 
       type = "folder", 
       name = "cat", 
       path = "/animals/cat", 
       children = new List<Node> 
       { 
        new Node 
        { 
         type = "folder", 
         name = "images", 
         path = "/animals/cat/images", 
         children = new List<Node> 
         { 
          new Node 
          { 
           type = "file", 
           name = "cat001.jpg", 
           path = "/animals/cat/images/cat001.jpg" 
           }, 
          new Node { 
           type = "file", 
           name = "cat001.jpg", 
           path = "/animals/cat/images/cat002.jpg" 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
}; 
string json = Jil.JSON.Serialize(obj, Jil.Options.PrettyPrint); 
+0

謝謝!這幫助了我的問題! – jessemillman 2014-09-24 10:17:38

0

當你說C# object我這樣想,沒有自定義的類都有涉及。 您可以使用dynamic

var serializer = new JavaScriptSerializer(); 
serializer.RegisterConverters(new[] { new DynamicJsonConverter() }); 

dynamic obj = serializer.Deserialize(e.Parameters, typeof(object)); 

然後你就可以像訪問屬性:

string type = obj.type as string; 
string name = obj.name as string; 
... 

DynamicJsonConverter的代碼可以發現here