将Tree类转换为更简单的Tree类,并序列化为Json维护结构 [英] Convert a Tree Class into a simpler Tree class and Serialize to Json maintaining structure

查看:85
本文介绍了将Tree类转换为更简单的Tree类,并序列化为Json维护结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我有一个与此类似的课程:

ok, so i have a class similar to this:

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)

i also have a List<Channels> (with a total of about 650 Channels)

ROOT频道包含大约6个频道,每个频道包含,子级等等.

the ROOT channel contains about 6 channels and each of the channels contains, children and so on.

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

as you see in the code of Channel there are a lot of other properties, which i don't want to serialize IN THIS CASE. that is, all the Data Contracts are already defined in the base class, and i do not/can not change them just for this action.

那么,你问我什么问题? 我需要将List<Channels>List<NewType>序列化为JSON并维护Tree结构.

so, what is my problem you ask? i need to get a List<Channels> or List<NewType> to be serialized to JSON and maintain the Tree structure.

如果不可能,至少如何将List<Channels>序列化为JSON来保持结构?

if it is not possible, at least, how would i serialize the List<Channels> to JSON maintaining the structure?

我正在使用Newtonsoft.Json

推荐答案

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

Personally I would suggest serializing the list to an array of Channels...consider the following JSON example:

{
    "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传递后的测试结果

Here is the result of that test after being passed through 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
        }
    ]
}

有效的JSON!

我注意到的一件事是JavaScriptSerializer不会解析"Channel",因为整个对象的类型都是Channel,而"Children"中的每个对象的类型都是Channel.如果要通过Deserialize方法传递此JSON,则应该将其重新构建为具有所有JSON持久数据的C#结构.

One thing I have noticed is that JavaScriptSerializer does not parse "Channel", since the whole object is of type Channel and each object in "Children" is of type Channel. If you were to pass this JSON through the Deserialize method, if should build this back into a C# structure with all JSON persisted data.

这篇关于将Tree类转换为更简单的Tree类,并序列化为Json维护结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆