-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?

3
  • at the end can you not use ].ToList(); also is this a JSON string..? Commented Nov 17, 2014 at 16:59
  • That doesn't look like a multidimensional array to me, even for JSON. It looks more like an array of class instances. Commented Nov 17, 2014 at 17:01
  • What is GenMenuList? Does it have a child property? Is the child property also a GenMenuList? Please see my answer below. Commented Nov 17, 2014 at 17:31

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.