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.
cdSectionadynamicor assign arbitrary C#-type properties to it. If you want to add a property to aJObject, usecdSection.Add("section", theValue)sectionproperty of typeCDJson?