Seven price
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
The way it should render is like this below.
class
Current code renders like
[
{
id: 1,
name: 'root1',
children: [
"id:" 2, name: 'child1' ,
"id:" 3, name: 'child2'
]
}
];
The way it should render is like this below.
[
{
id: 1,
name: 'root1',
children: [
{ id: 2, name: 'child1' },
{ id: 3, name: 'child2' }
]
}
];
properties.
public int id { get; set; }
public string name { get; set; }
public IList<string> children { get; set; }
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++;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
var attachmentsList = new List<RootObject>();
attachmentsList.Add(new RootObject
{
id = i + 1,
name = "Folder" + i + 1,
children = new List<Child>()
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>()
});
ASKER
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.
ASKER
thanks
i got it.
children = new List<Child>()
};
rootObject.children.Add(ne w Child { id = i + 1, name = file.Name });
i++;
i got it.
children = new List<Child>()
};
rootObject.children.Add(ne
i++;
ASKER