【问题标题】:How to hardcode a Multidimensional Array/List in C#?如何在 C# 中硬编码多维数组/列表?
【发布时间】:2015-01-14 15:59:53
【问题描述】:

我在搜索时找不到一个简单的例子。我的数组在Javascript中如下:

            var body = [ 
            {
                "key": "1",
                "name": "folder 1",
                "child": {
                    "key": "5",
                    "name": "(1) nested file",
                    "child": {
                        "key": "12",
                        "name": "(1) nested  file"
                    }
                }
            },
            {
                "key": "2",
                "name": "folder 2",
                "child": {
                    "key": "6",
                    "name": "(1) nested file"
                }
            }
        ];

如何将其转换为 C# 的字符串/列表?所以:

List<DTO.GenMenuList> body= new List<DTO.GenMenuList>(); 
body.Add(new DTO.GenMenuList() {key=1,name="folder 1"});

body.Add(new DTO.GenMenuList() {key=2, name="folder 2"});

将开始填充我列表的父级,但如何嵌套子级?

【问题讨论】:

  • 最后你能不能不用].ToList(); 这也是JSON 字符串..?
  • 对我来说这看起来不像是一个多维数组,即使对于 JSON 也是如此。它看起来更像是一个类实例数组。
  • 什么是 GenMenuList?它有子属性吗?子属性也是 GenMenuList 吗?请在下面查看我的答案。

标签: javascript c# arrays string list


【解决方案1】:

Json.NET(来自 Newtonsoft)是一种序列化/反序列化 JSON 数据的好方法。您可以通过 Visual Studio 中的 NuGet 获取它。请参阅this 了解如何引用它。

根据 BenjaminPaul 的回答,您可以执行以下操作:

        public class Item
        {
            public string key { get; set; }
            public string name { get; set; }
            public Item child { get; set; }

            public Item(string ItemKey, string ItemName)
            {
                this.key = ItemKey;
                this.name = ItemName;
            }
        }

        public static void Test()
        {
            List<Item> data = new List<Item>();

            Item key1 = new Item("1", "folder 1");
            data.Add(key1);

            Item key5 = new Item("5", "(1) nested file");
            key1.child = key5;

            Item key12 = new Item("12", "(1) nested file");
            key5.child = key12;

            Item key2 = new Item("2", "folder 2");
            data.Add(key2);

            Item key6 = new Item("6", "(1) nested file");
            key2.child = key6;

            string serialized = Newtonsoft.Json.JsonConvert.SerializeObject(data);
            List<Item> deserialized = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Item>>(serialized);
        }

名为“serialized”的字符串可能就是你要找的。​​p>

【讨论】:

    【解决方案2】:

    这只是一个类实例的数组,所以它不是多维的。

    这将解决您的问题...

    public class Item {
        public int Key { get; set; }
        public string Name { get; set; }
        public Item Child { get; set; }
    }
    
    List<Item> myList = new List<Item>();
    

    当然,如果项目没有子项,则子项可以为 null,如果您要将此列表序列化为 Javascript,您将获得您的数组。

    还有这个网站:http://json2csharp.com/ 会从你的 json 为你生成 pocos ......但是我想说我上面的代码要好一些。 (你不需要第二个孩子)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-18
      • 2013-05-17
      • 2019-05-04
      • 2010-12-08
      • 2010-10-27
      • 2013-02-21
      • 2019-09-18
      • 1970-01-01
      相关资源
      最近更新 更多