C# 将树类转换为更简单的树类,并序列化为Json结构
好的,我有一个类似的类:C# 将树类转换为更简单的树类,并序列化为Json结构,c#,json,tree,json.net,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;} } 公共类频道 { 公共
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;}
}
公共类频道
{
公共Guid Guid{get;set;}
公共字符串标题{get;set;}
/*在这个地方有更多的财产*/
公共Guid父项{get;set;}
公共列表子项{get;set;}
}
我还有一个列表
(总共有大约650个频道
)
ROOT
频道包含大约6个频道,每个频道包含、子频道等
正如您在频道的代码中看到的那样
还有许多其他属性,
在这种情况下,我不想序列化它。也就是说,所有的数据契约
都已经在基类中定义,我不/不能仅仅为了这个操作而更改它们
那么,你问我有什么问题?
我需要获取一个列表
或列表
,将其序列化为JSON
,并维护树结构
如果不可能,至少,我如何将列表
序列化为JSON
以维护结构
编辑:
我使用的是<代码> NethOpth.JSON/COD> < P>个人,我建议将列表序列化为一个通道数组……考虑下面的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
}
}
]
}
}
EDIT:下面是一个小测试代码,它将编写这个结构:
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; }
}
}
有效的JSON
编辑:我注意到的一件事是JavaScriptSerializer不解析“Channel”,因为整个对象是Channel类型,而“Children”中的每个对象都是Channel类型。如果要通过反序列化方法传递此JSON,则应将其构建回包含所有JSON持久化数据的C#结构。您是否考虑过将列表序列化为Channel类型的数组?这给了我一个平面数组,而我需要保留该结构。请看我的答案。它维护着一系列的渠道结构……这不是你要问的吗?这就是你要说的问题吗?我不是NewtonSoft程序员,所以我帮不了你。你能展示一些代码来实现这一点吗?这就是我想做的,但我不知道怎么做…@Dementic,当然…再看看我的答案@Dementic,我注意到您已经编辑过说您正在使用Newtonsoft.JSON…我已经使用Microsoft内置的JSON序列化程序编写了我的示例。如果微软的序列化程序能做到这一点,我相信Newtonsoft的序列化程序也能做到!。。。但是,我没有使用JSON.NET的经验,因为我已经构建了自己的个人JSON序列化程序/反序列化程序。这很好,但是,我最初的频道
子频道有“[XmlIgnore]”属性,我无法更改。@Dementic…我不确定为什么[XmlIgnore]会被JSON序列化程序拾取,因为XML和JSON是不同的格式?…让我检查一下
{
"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
}
]
}