C# umbraco内容树节点转换为json格式
我有一个umbraco网站,内容结构如下: (括号中的文本是nodeTypeAlias) 我正在尝试将上述结构(以及节点的属性)导出到以下json文件中:C# umbraco内容树节点转换为json格式,c#,umbraco,C#,Umbraco,我有一个umbraco网站,内容结构如下: (括号中的文本是nodeTypeAlias) 我正在尝试将上述结构(以及节点的属性)导出到以下json文件中: { "root": { "child": [ { "id": "1", "name": "Child 1", "module": [ {
{
"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;}
有什么想法吗
谢谢。这是生成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()) @{ var builder=new System.Text.StringBuilder(); foreach(车辆中的var车辆) { 附加(“{”); builder.Append(Json.Encode(“reg”)+”:“+Json.Encode(car.Registration)+”,”; Append(Json.Encode(“model”)+:“+car.model”); //在此处插入其他值 builder.Append(“}”); if(car.Registration!=cars.Last().Registration) { 生成器。追加(“,”); } 计数++; } } @Html.Raw(builder.ToString()) 另一个更有趣的方法是JSON.NET——如果您在应用程序中使用自定义对象,这是一个很好的选择。填充对象,然后通过JSON.NET传递它们 产品=新产品(); product.Name=“苹果”; 产品有效期=新的日期时间(2008年12月28日); 产品价格=399万元; product.size=新字符串[]{“小”、“中”、“大”}; 字符串json=JsonConvert.SerializeObject(产品); //{ //“名称”:“苹果”, //“到期日”:“2008-12-28:00:00”, //“价格”:3.99, //“尺寸”:[ //“小”, //“中等”, //“大” // ] //} 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 字符串json=@”{ “名称”:“苹果”, “到期日”:“2008-12-28:00:00”, “价格”:3.99, “尺寸”:[ “小”, “中等”, “大” ] }"; JObject o=JObject.Parse(json); 字符串名称=(字符串)o[“名称”]; //苹果 JArray大小=(JArray)o[“大小”]; 字符串最小=(字符串)大小[0]; //小的 如果使用System.Xml名称空间(XmlDocument、XmlAttributes、XmlNode),则代码可以工作。不清楚您使用的是哪个节点对象,但我将假定它是Umbraco的节点(顺便说一句,我将使用一个新的DynamicNode()在这种情况下,您不能像创建填充属性那样创建它们。您必须在读取其值之前在Umbraco数据库中创建一个 希望这会给你一些可以继续的东西。快乐的编码 @{ 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()) 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); 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