-1

I cannot find a straightforward example of this upon my searches. My array is as follows in 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"
                }
            }
        ];

how do I convert this to a string/list for C#? So:

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

will start to populate the parents of my list, but how do I nest the children?

triplethreat77
  • 1,276
  • 8
  • 34
  • 69

2 Answers2

2

This is simply an array of class instances so it is not multi dimensional.

This would solve your issue...

public class Item {
    public int Key { get; set; }
    public string Name { get; set; }
    public Item Child { get; set; }
}

List<Item> myList = new List<Item>();

Child can be null if the item has no children of course, if you were to serialize this list to Javascript you would get your array.

There is also this website: http://json2csharp.com/ that will generate pocos for you from your json... however my code above is a little better I would say. (You dont need a second child).

BenjaminPaul
  • 2,931
  • 19
  • 18
0

Json.NET (from Newtonsoft) is a good way of serializing/deserializing JSON data. You can get it via NuGet in Visual Studio. See this for how to reference it.

Building on BenjaminPaul's answer, you can do the following:

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

The string named "serialized" is probably what you're looking for.

Community
  • 1
  • 1
wlemond
  • 94
  • 4