在字典和转换中将自定义类添加为列表

时间:2014-02-20 08:45:33

标签: c# json dictionary

嗨,我想要一些东西,我有自己的自定义类menuestructure,我想先制作和列出这种类型,并将创建多个对象,然后想要传递和添加字典,然后也转换为Json必须是感谢有人可以帮助我。 这里是我的代码与评论,实际上我想要的,如果在代码中添加一点代码,我将如何更好地实现它。

代码: -

public string GetMenue()
{
    List<MenueStructure> chlidObj1 = new List<MenueStructure>();
    chlidObj1.Add(new MenueStructure { ID = "1", ParentID = "", ChildID = "1", MenuName = "Registration", MenuLink = "Registration.aspx"});
    chlidObj1.Add(new MenueStructure { ID = "2", ParentID = "1", ChildID = "2", MenuName = "Assign Subject", MenuLink = "AssignSubject.aspx"});

    List<MenueStructure> chlidObj2 = new List<MenueStructure>();
    chlidObj2.Add(new MenueStructure { ID = "1", ParentID = "", ChildID = "1", MenuName = "Registration", MenuLink = "Registration.aspx"});
    chlidObj2.Add(new MenueStructure { ID = "2", ParentID = "1", ChildID = "2", MenuName = "Assign Subject", MenuLink = "AssignSubject.aspx"});

    Dictionary<string, List<MenueStructure>> childs = new Dictionary<string, MenueStructure[]>()
             {
                 // here error no overload method to take one argument
                 childs.Add("TEACHERS", chlidObj1);
                 childs.Add("TEACHERS", chlidObj1);
             };

然后我想从这本字典创建Json怎么可能。 并以字符串形式返回。帮帮我这个怎么办 }

2 个答案:

答案 0 :(得分:0)

您正在以错误的方式初始化词典。您可以先初始化并清空字典并在之后添加项目。

Dictionary<string, List<MenueStructure>> childs = new Dictionary<string, MenueStructure[]>();

childs.Add("TEACHERS", chlidObj1);
childs.Add("TEACHERS", chlidObj1);

您可以在初始化词典时添加项目

Dictionary<string, List<MenueStructure>> childs = new Dictionary<string, MenueStructure[]>()
{
    { "TEACHERS", chlidObj1},
    { "TEACHERS", chlidObj1}
};

答案 1 :(得分:0)

Dictionary转换为Json,反之亦然,因为您必须在项目中安装和使用Json.Net库。

序列化词典:

Dictionary<string, List<MenueStructure>> childs = new Dictionary<string, List<MenueStructure>>
                                                              {
                                                                  {"TEACHERS", chlidObj1},
                                                                  {"STUDENTS", chlidObj2}
                                                              };

string json = JsonConvert.SerializeObject(childs, Formatting.Indented);

反序列化词典:

var deserializeObject = JsonConvert.DeserializeObject<Dictionary<string, List<MenueStructure>>>(json);

所以你的GetMenue()如下所示:

public string GetMenue()
{
    List<MenueStructure> chlidObj1 = new List<MenueStructure>();
    chlidObj1.Add(new MenueStructure
                          {
                              ID = "1",
                              ParentID = "",
                              ChildID = "1",
                              MenuName = "Registration",
                              MenuLink = "Registration.aspx"
                          });

    chlidObj1.Add(new MenueStructure
                          {
                              ID = "2",
                              ParentID = "1",
                              ChildID = "2",
                              MenuName = "Assign Subject",
                              MenuLink = "AssignSubject.aspx"
                          });

    List<MenueStructure> chlidObj2 = new List<MenueStructure>();
    chlidObj2.Add(new MenueStructure
                          {
                              ID = "1",
                              ParentID = "",
                              ChildID = "1",
                              MenuName = "Registration",
                              MenuLink = "Registration.aspx"
                          });
    chlidObj2.Add(new MenueStructure
                          {
                              ID = "2",
                              ParentID = "1",
                              ChildID = "2",
                              MenuName = "Assign Subject",
                              MenuLink = "AssignSubject.aspx"
                          });

    Dictionary<string, List<MenueStructure>> childs = new Dictionary<string, List<MenueStructure>>
                                                              {
                                                                  {"TEACHERS", chlidObj1},
                                                                  {"STUDENTS", chlidObj2}
                                                              };

    string json = JsonConvert.SerializeObject(childs, Formatting.Indented);
    var deserializeObject = JsonConvert.DeserializeObject<Dictionary<string, List<MenueStructure>>>(json);
    return json;