Convert a Tree Class into a simpler Tree class and Serialize to Json maintaining structure

强颜欢笑 提交于 2019-12-08 15:02:50

问题


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

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

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

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.

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.

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

EDIT: i am using Newtonsoft.Json


回答1:


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
                }
            }
        ]
    }
}

EDIT: Here is a little test code which will write this structure:

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

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
        }
    ]
}

VALID JSON!

EDIT: 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.



来源:https://stackoverflow.com/questions/13232306/convert-a-tree-class-into-a-simpler-tree-class-and-serialize-to-json-maintaining

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!