Link to home
Start Free TrialLog in
Avatar of Seven price
Seven priceFlag for United States of America

asked on

fomat Json objects

in c# need to create this structure.  Adding the children object I seem to be having trouble with children property  to render below.
Current code renders like
 [
        {
            id: 1,
            name: 'root1',
            children: [
               "id:" 2, name: 'child1' ,
               "id:" 3, name: 'child2' 
            ]
        }
    ];

Open in new window



The way it should render is like this below.
 [
        {
            id: 1,
            name: 'root1',
            children: [
                { id: 2, name: 'child1' },
                { id: 3, name: 'child2' }
            ]
        }
    ];

Open in new window


properties.

        public int id { get; set; }
        public string name { get; set; }
        public IList<string> children { get; set; }

Open in new window



class

 var attachmentsList = new List<ModelDto>();
 FileInfo[] files = dinfo.GetFiles();
                
                foreach (FileInfo file in files)
                {
                    attachmentsList.Add(new ModelDto
                    {
                      
                        id = i + 1,
                        name = "Folder " + i + 1,
                        children = new List<string>
                         {
                            id = i + 1,
                            name = file.name,
                   
                        }
                    });
     
                    i++;

                }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Daniel Van Der Werken
Daniel Van Der Werken
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Seven price

ASKER

Thanks Daniel. Question Creating the properties not the problem the code in the correct format to display
 var attachmentsList = new List<RootObject>();
 attachmentsList.Add(new RootObject
                    {
                        id = i + 1,
                        name = "Folder" + i + 1,
                        children = new List<Child>()

Open in new window

Ok this is where I get stuck at. Cannot add the name and the id inside the children also.
This?

            var attachmentList = new List<RootObject>();
            attachmentList.Add(
                new RootObject()
                {
                    id = i + 1,
                    name = "Folder" + (i + 1).ToString(),
                    children = new List<Child>()
                });

Open in new window

yes but   children = new List<Child>() cannot add the other properties. tryied the other ways in json.net but since I have to return a list the other ways will not let me.
thanks
i got it.
    children = new List<Child>()
                    };
                    rootObject.children.Add(new Child { id = i + 1, name = file.Name });
                    i++;