我有一个umbraco网站,其内容结构如下: (括号中的文本是nodeTypeAlias)
[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 :(得分:0)
这就是生成XML的方式,但JSON有点不同。如果您已将Umbraco更新为其中一个版本,则使用Razor以这种方式呈现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),则代码可以正常工作。目前还不清楚你正在使用哪个Node对象,但我会假设它是Umbraco的Node(我会使用新的DynamicNode()而不是btw),在这种情况下,你不能像使用填充属性那样创建它们。在读取其值之前,您必须在Umbraco数据库中创建一个。
希望这可以给你一些东西继续下去。 快乐编码。