将树类转换为更简单的Tree类,并将Serialize转换为Json维护结构

时间:2012-11-05 12:38:06

标签: c# json tree json.net

好的,所以我有一个类似的类:

public class Channel
{
    public Guid Guid{get;set;}
    public string Title{get;set;}

    /* GOT A LOT MORE PROPERTIES IN THIS SPACE */

    public Guid Parent {get;set;}
    public List<Channel> Children{get;set;}
}

我还有List<Channels>(总共大约650 Channels

ROOT频道包含大约6个频道,每个频道包含,儿童等。

正如您在Channel的代码中看到的,还有很多其他属性, 我不想在这种情况下序列化。也就是说,所有Data Contracts都已在基类中定义,并且我不会/不能仅为此操作更改它们。

所以,你问我的问题是什么? 我需要将List<Channels>List<NewType>序列化为JSON并维护树结构。

如果不可能,至少如何序列化List<Channels>JSON维护结构?

修改 我正在使用Newtonsoft.Json

1 个答案:

答案 0 :(得分:1)

我个人建议将列表序列化为一个Channels数组...考虑以下JSON示例:

{
    "Channel": {
        "Guid": "27182d04-29d9-4760-86d5-484ba43cd9c6",
        "Title": "FooBar",
        "Parent": "52cfd532-6010-41c5-8fa9-f3bcbb97a630",
        "Children": [
            {
                "Channel": {
                    "Guid": "27182d04-29d9-4760-86d5-484ba43cd9c6",
                    "Title": "FooBar",
                    "Parent": "52cfd532-6010-41c5-8fa9-f3bcbb97a630",
                    "Children": null
                }
            },
            {
                "Channel": {
                    "Guid": "27182d04-29d9-4760-86d5-484ba43cd9c6",
                    "Title": "FooBar",
                    "Parent": "52cfd532-6010-41c5-8fa9-f3bcbb97a630",
                    "Children": null
                }
            },
            {
                "Channel": {
                    "Guid": "27182d04-29d9-4760-86d5-484ba43cd9c6",
                    "Title": "FooBar",
                    "Parent": "52cfd532-6010-41c5-8fa9-f3bcbb97a630",
                    "Children": null
                }
            }
        ]
    }
}

编辑:这是一个用于编写此结构的测试代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;

namespace JSONTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Channel c = new Channel();
            c.Guid = Guid.NewGuid();
            c.Parent = Guid.NewGuid();
            c.Title = "FooBar";
            c.Children = new List<Channel>();

            Channel a = new Channel();
            Channel b = new Channel();

            a.Guid = Guid.NewGuid();
            b.Guid = Guid.NewGuid();

            a.Parent = Guid.NewGuid();
            b.Parent = Guid.NewGuid();

            a.Title = "FooBar_A";
            b.Title = "FooBar_B";

            c.Children.Add(a);
            c.Children.Add(b);

            /* Serialization happens here!! */
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string result = serializer.Serialize(c);
            Console.WriteLine(result);
            Console.Read();
        }
    }

    public class Channel
    {
        public Guid Guid { get; set; }
        public string Title { get; set; }

        /* GOT A LOT MORE PROPERTIES IN THIS SPACE */

        public Guid Parent { get; set; }
        public List<Channel> Children { get; set; }
    }
}

以下是通过JSONLint

传递后的测试结果
{
    "Guid": "0e72c12c-a7a1-461a-8b84-8b17023e2e2f",
    "Title": "FooBar",
    "Parent": "d0943246-1adc-4208-bb3b-1249ffe5e7b4",
    "Children": [
        {
            "Guid": "1cf413be-d6b5-405e-8308-7c6dfe817f9a",
            "Title": "FooBar_A",
            "Parent": "ecf14fce-c97d-46f5-890b-bab8ff99fb4a",
            "Children": null
        },
        {
            "Guid": "bd96e6d3-f247-4a0d-9147-92da19793e97",
            "Title": "FooBar_B",
            "Parent": "5cd3e765-23c2-4145-8b45-9964a7c66c54",
            "Children": null
        }
    ]
}

VALID JSON!

编辑:我注意到的一件事是JavaScriptSerializer不解析“Channel”,因为整个对象是Channel类型,“Children”中的每个对象都是Channel类型。如果您通过Deserialize方法传递此JSON,则应将其构建回具有所有JSON持久数据的C#结构。