2012-10-23 71 views
1

我有一把umbraco網站,其內容如下結構: (括號中的文字是nodeTypeAlias)我試圖用出口上述結構(一起一把umbraco內容節點,以JSON格式

[root] 
- [child] 
| - [module] 
| | - [submodule] 
| | - [submodule] 
- [child] 
| - [module] 
| - [module] 
| | - [submodule] 

節點的屬性)到以下JSON文件:

{ 
    "root": { 
     "child": [ 
      { 
       "id": "1", 
       "name": "Child 1", 
       "module": [ 
        { 
         "id": "2", 
         "name": "Module 1", 
         "submodule": [ 
          { 
           "id": "3", 
           "name": "Sub module 1" 
          }, 
          { 
           "id": "4", 
           "name": "Sub module 3" 
          } 
         ] 
        } 
       ] 
      }, 
      { 
       "id": "5", 
       "name": "Child 5", 
       "module": [ 
        { 
         "id": "8", 
         "name": "Module 8" 
        }, 
        { 
         "id": "6", 
         "name": "Module 6", 
         "submodule": [ 
          { 
           "id": "7", 
           "name": "Sub module 7" 
          }, 
          { 
           "id": "9", 
           "name": "Sub module 9" 
          } 
         ] 
        } 
       ] 
      } 
     ] 
    } 
} 

到目前爲止,我寫下了下面的代碼在Linqpad但結果不是我一直在尋找的人。

List<Node> brands = new List<Node>() 
{    
    new Node 
    { 
     id = 1, 
     name = "Brand 1", 
     type = "brand", 
     children = new List<Node> 
     {   
      new Node 
      { 
       id = 2, 
       name = "Shelf 1", 
       type = "shelf", 
       children = new List<Node> 
       { 
        new Node 
        { 
         id = 1, 
         name = "Bookcase 1", 
         type = "bookcase" 
        }, 
        new Node 
        { 
         id = 2, 
         name = "Bookcase 2", 
         type = "bookcase" 
        }, 
        new Node 
        { 
         id = 3, 
         name = "Bookcase 3", 
         type = "bookcase" 
        } 
       } 
      }, 
      new Node 
      { 
       id = 3, 
       name = "Shelf 2", 
       type = "shelf", 
       children = new List<Node> 
       { 
        new Node 
        { 
         id=1, 
         type= "module", 
         name = "Module 1" 
        }, 
        new Node 
        { 
         id=2, 
         type= "pdf", 
         name = "PDF 1" 
        }, 
        new Node 
        { 
         id=3, 
         type= "link", 
         name = "Link 1" 
        }, 
        new Node 
        { 
         id=4, 
         type= "link", 
         name = "Link 2" 
        }, 
       } 
      }     
     } 
    }, 

    new Node 
    { 
     id = 2, 
     name = "Brand 2", 
     type = "brand", 
     children = new List<Node> 
     { 
      new Node 
      { 
       id = 2, 
       name = "Shelf 1", 
       type = "shelf", 
      }, 
      new Node 
      { 
       id = 3, 
       name = "Shelf 2", 
       type = "shelf", 
      } 
     } 
    } 
}; 


Result container = new Result(); 

Action<List<Node>, Result> add = null; 
add = (nodes, coll) => 
{ 
    List<Result> list = null; 
    if(nodes != null && nodes.Count > 0) 
    { 
     nodes.Dump("nodes"); 
     foreach(var node in nodes) 
     { 
      string key = node.type; 
      list = null; 
      if(coll.Children.ContainsKey(key)) 
      { 
       list = coll.Children[key]; 
      } 
      else 
      { 
       list = new List<Result>(); 
      } 
      Result r = new Result(); 
      r.Name = node.name; 
      add(node.children, r); 
      list.Add(r); 
      coll.Children[key] = list; 
      coll.Dump("coll"); 
     } 
    } 
}; 

add(brands, container); 
container.Dump(); 
var serializer = new JavaScriptSerializer(); 
serializer.Serialize(container).Dump(); 
} 

public class Result 
{ 
    public Result() 
    { 
     this.Children = new Dictionary<string, List<Result>>(); 
     this.Properties = new Dictionary<string, string>(); 
    } 
    public string Name {get;set;} 
    public Dictionary<string, string> Properties {get;set;} 
    public Dictionary<string, List<Result>> Children {get;set;} 
} 

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

結果:

{ 
    "Name": null, 
    "Properties": {}, 
    "Children": { 
     "brand": [ 
      { 
       "Name": "Brand 1", 
       "Properties": {}, 
       "Children": { 
        "shelf": [ 
         { 
          "Name": "Shelf 1", 
          "Properties": {}, 
          "Children": { 
           "bookcase": [ 
            { 
             "Name": "Bookcase 1", 
             "Properties": {}, 
             "Children": {} 
            }, 
            { 
             "Name": "Bookcase 2", 
             "Properties": {}, 
             "Children": {} 
            }, 
            { 
             "Name": "Bookcase 3", 
             "Properties": {}, 
             "Children": {} 
            } 
           ] 
          } 
         }, 
         { 
          "Name": "Shelf 2", 
          "Properties": {}, 
          "Children": { 
           "module": [ 
            { 
             "Name": "Module 1", 
             "Properties": {}, 
             "Children": {} 
            } 
           ], 
           "pdf": [ 
            { 
             "Name": "PDF 1", 
             "Properties": {}, 
             "Children": {} 
            } 
           ], 
           "link": [ 
            { 
             "Name": "Link 1", 
             "Properties": {}, 
             "Children": {} 
            }, 
            { 
             "Name": "Link 2", 
             "Properties": {}, 
             "Children": {} 
            } 
           ] 
          } 
         } 
        ] 
       } 
      }, 
      { 
       "Name": "Brand 2", 
       "Properties": {}, 
       "Children": { 
        "shelf": [ 
         { 
          "Name": "Shelf 1", 
          "Properties": {}, 
          "Children": {} 
         }, 
         { 
          "Name": "Shelf 2", 
          "Properties": {}, 
          "Children": {} 
         } 
        ] 
       } 
      } 
     ] 
    } 
} 

任何想法?

謝謝。

回答

0

這非常適合您生成XML的方式,但JSON有點不同。如果你更新了你一把umbraco到後來的版本之一,那麼你用剃刀呈現JSON在這種方式:

 
    @{ 
    var builder = new System.Text.StringBuilder();    
    foreach (var car in cars) 
    {    
    builder.Append("{"); 
    builder.Append(Json.Encode("reg") + ":" + Json.Encode(car.Registration) + ","); 
    builder.Append(Json.Encode("model") + ":" + car.Model); 

    // INSERT OTHER VALUES HERE  

    builder.Append("}"); 
    if (car.Registration != cars.Last().Registration) 
    { 
    builder.Append(","); 
    } 
    count++; 
    } 
    } 
    @Html.Raw(builder.ToString()) 

另一個更有趣的方式是JSON.NET - 一個很好的選擇,如果你正在使用自定義對象在您的應用程序。你填充你的對象,然後通過JSON.NET傳遞它們。 See here for more details

 
Product product = new Product(); 
product.Name = "Apple"; 
product.Expiry = new DateTime(2008, 12, 28); 
product.Price = 3.99M; 
product.Sizes = new string[] { "Small", "Medium", "Large" }; 

string json = JsonConvert.SerializeObject(product); 
//{ 
// "Name": "Apple", 
// "Expiry": "2008-12-28T00:00:00", 
// "Price": 3.99, 
// "Sizes": [ 
// "Small", 
// "Medium", 
// "Large" 
// ] 
//} 

Product deserializedProduct = JsonConvert.DeserializeObject(json); 

這個庫的好處是,你還可以創建字符串,並將其轉換成JSON。唯一需要注意的是,當您嘗試閱讀JSON時,您必須觀看JavaScript。如單(')和雙引號(「)的東西可以破壞你的代碼。

 
string json = @"{ 
    ""Name"": ""Apple"", 
    ""Expiry"": "2008-12-28T00:00:00", 
    ""Price"": 3.99, 
    ""Sizes"": [ 
    ""Small"", 
    ""Medium"", 
    ""Large"" 
    ] 
}"; 

JObject o = JObject.Parse(json); 

string name = (string)o["Name"]; 
// Apple 

JArray sizes = (JArray)o["Sizes"]; 

string smallest = (string)sizes[0]; 
// Small 

,如果你使用的System.Xml命名空間,您的代碼可以工作(XmlDocument的,XMLATTRIBUTES,XmlNode的)。目前尚不清楚哪個你正在使用的節點對象,但我會認爲它是Umbraco的節點(我會用一個新的DynamicNode()而不是btw),在這種情況下,你不能像創建它們那樣使用填充屬性來創建它們,你必須在Umbraco數據庫中創建一個前讀取其值。

這應該可以給你的東西一起去。快樂編碼