1

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

任何想法?

谢谢。

4

1 回答 1

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);

    // 在此处插入其他值     

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

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

产品产品 = 新产品();
product.Name = "苹果";
product.Expiry = new DateTime(2008, 12, 28);
产品.价格 = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };

字符串 json = JsonConvert.SerializeObject(product);
//{
// "名称": "苹果",
// "到期": "2008-12-28T00:00:00",
// "价格": 3.99,
//“尺寸”:[
// “小的”,
// “中等的”,
// “大的”
// ]
//}

产品反序列化产品 = JsonConvert.DeserializeObject(json);

这个库的美妙之处在于您还可以创建字符串并将它们转换为 JSON。唯一需要注意的是,在尝试读取 JSON 时必须注意 JavaScript。单引号 (') 和双引号 (") 可能会破坏您的代码。

字符串 json = @"{
  ""名称"": ""苹果"",
  ""到期"": "2008-12-28T00:00:00",
  ""价格"": 3.99,
  ""尺寸"": [
    ““小的””,
    ““中等的””,
    ““大的””
  ]
}";

JObject o = JObject.Parse(json);

字符串名称 = (string)o["名称"];
// 苹果

JArray 大小 = (JArray)o["Sizes"];

字符串最小 = (string)sizes[0];
// 小的

如果您使用 System.Xml 命名空间(XmlDocument、XmlAttributes、XmlNode),您的代码就可以工作。目前尚不清楚您正在使用哪个 Node 对象,但我会假设它是 Umbraco 的 Node(我将使用 new DynamicNode() 而不是 btw),在这种情况下,您不能像使用填充属性那样创建它们。在读取它的值之前,您必须在 Umbraco 数据库中创建一个。

希望这能给你一些东西。快乐编码

于 2013-04-17T22:23:06.107 回答