我试图动态构建我的json,但似乎无法在动态对象中获得结果来存储值:
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;
}代码:
var CD_Section = build.Build_CDSectionJson(sectionGUID, "", "sectioncon")输出:
{"section":{}}我正在传递一些值,但它们是空的,我做错了什么?
我试过这个:
dynamic cdSection = new JObject();
cdSection.section.id = id;
cdSection.section.style = style;
cdSection.section.@class = @class;但返回的结果为空。
发布于 2019-12-08 21:00:23
谢谢JLRishe
这样做效果很好:
JObject cdSectionAttr = new JObject();
cdSectionAttr.Add("id", id);
cdSectionAttr.Add("style", style);
cdSectionAttr.Add("@class", @class);
JObject cdSection = new JObject();
cdSection.Add("section", cdSectionAttr);https://stackoverflow.com/questions/59235419
复制相似问题