Reputation: 2297
I am trying to build my json dynamically but cant seem to get the results within the dynamic object to store the values:
public class CDJson : JObject
{
public string id { get; set; }
public string style { get; set; }
public string @class { get; set; }
}
public class CDColsJson : JObject
{
public string id { get; set; }
public string style { get; set; }
public string @class { get; set; }
public int xsCol { get; set; }
public int smCol { get; set; }
public int mdCol { get; set; }
public int lgCol { get; set; }
}
public string Build_CDSectionJson(string id, string style, string @class)
{
dynamic cdSection = new JObject();
cdSection.section = new Constants.CDJson() { id = id, style = style, @class = @class };
string json = JsonConvert.SerializeObject(cdSection); // WHERE THE VALUES ARE EMPTY
return json;
}
Code:
var CD_Section = build.Build_CDSectionJson(sectionGUID, "", "sectioncon")
Output:
{"section":{}}
I am passing some values but they are empty, what am i doing wrong?
I tried this:
dynamic cdSection = new JObject();
cdSection.section.id = id;
cdSection.section.style = style;
cdSection.section.@class = @class;
But came back as null.
Upvotes: 1
Views: 72
Reputation: 2297
Thanks JLRishe
This worked great:
JObject cdSectionAttr = new JObject();
cdSectionAttr.Add("id", id);
cdSectionAttr.Add("style", style);
cdSectionAttr.Add("@class", @class);
JObject cdSection = new JObject();
cdSection.Add("section", cdSectionAttr);
Upvotes: 1