Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 48
  • Last Modified:

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

0
Seven price
Asked:
Seven price
  • 4
  • 2
1 Solution
 
Daniel Van Der WerkenCommented:
This is the site: http://json2csharp.com/#


public class Child
{
    public int id { get; set; }
    public string name { get; set; }
}

public class RootObject
{
    public int id { get; set; }
    public string name { get; set; }
    public List<Child> children { get; set; }
}

Open in new window

0
 
Seven priceFull StackAuthor Commented:
Thanks Daniel. Question Creating the properties not the problem the code in the correct format to display
0
 
Seven priceFull StackAuthor Commented:
 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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Daniel Van Der WerkenCommented:
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

0
 
Seven priceFull StackAuthor Commented:
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.
0
 
Seven priceFull StackAuthor Commented:
thanks
i got it.
    children = new List<Child>()
                    };
                    rootObject.children.Add(new Child { id = i + 1, name = file.Name });
                    i++;
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now