Umbraco内容树节点至JSON格式



我有一个带有以下内容结构的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": {}
                        }
                    ]
                }
            }
        ]
    }
}

有什么想法吗?

谢谢。

这是您生成XML的方式,但是JSON有些不同。如果您已将Umbraco更新为以后的版本之一,则使用剃须刀以这种方式渲染JSON:

   @{    var Builder = new System.Text.StringBuilder();    foreach(汽车中的var汽车)    {    Builder.Append(" {");    Builder.Append(JSON.ENCODE(" REG")  ":"   JSON.ENCODE(car.Registration)  ",");    Builder.Append(JSON.ENCODE(" Model")  ":"   CAR.MODEL);   //在此处插入其他值    Builder.Append("}");    if(car.Registration!= cars.last()。注册)    {    Builder.Append(",");    }    计数  ;    }    }    @html.raw(builder.tostring())

另一种更有趣的方法是JSON.NET-如果您在应用程序中使用自定义对象,则是一个不错的选择。您填充对象,然后将它们通过json.net。有关更多详细信息,请参见此处。

产品产品=新产品();product.name ="苹果";product.expiry =新DateTime(2008,12,28);Product.Price = 399m;product.sizes = new String [] {" Small"," Medive"," giald"};字符串json = jsonConvert.SerializeObject(product);//{//"名称":"苹果",//" expry":" 2008-12-28T00:00:00",//"价格":3.99,//"大小":[//   "小的",//   "中等的",//   "大的"//]//}产品Deserialized Product = JSONCONVERT.DeserializeObject(JSON);

这个库的美丽是您还可以创建字符串并将其转换为JSON。唯一的警告是,在尝试阅读JSON时,您必须观看JavaScript。诸如单(')和double(")引号之类的东西可能会破坏您的代码。

字符串json = @" {  "名称":"苹果,  "到期":" 2008-12-28T00:00:00",  "价格":3.99,  "大小":[[    "小的",    "中等的",    "大的"  这是给出的}";jobject o = jobject.parse(json);字符串名称=(string)o [" name"];//苹果jarray sizes =(jarray)o [" size"];字符串最小=(字符串)尺寸[0];//小的

如果您使用了System.xml名称空间(Xmldocument,XMLATTRIBUTES,XMLNODE),则代码可以工作。目前尚不清楚您使用的是哪个节点对象,但我会假设它是Umbraco的节点(我将使用一个新的DynamicNode()而不是btw),在这种情况下,您不能仅像填充属性那样创建它们。在阅读其值之前,您必须在Umbraco数据库中创建一个。

希望这应该给您一些事情。快乐编码

最新更新