c# - How do I append objects to a JSON variable? -
the issue here instance of class "obj" re-created every time run through loop @ end of loop, have 1 set of object. should have several.
foreach (var project in projectsdictionary) { foreach (var season in seasonsdictionary) { foreach (var episode in episodesdictionary) { obj = new parent { title = project.value, link = "1", children = new list<parent> { new parent { title = season.value, link = "1", children = new list<parent> { new parent { title = episode.value, link = "1", children = null } } } } }; } } } var responsebody = jsonconvert.serializeobject(obj); return responsebody; public class parent { public string title { get; set; } public string link { get; set; } public list<parent> children { get; set; } }
outside first loop define obj
list.
var obj = new list<parent>();
then
obj.add(new parent(...));
Comments
Post a Comment