首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JSON -动态构建Json

JSON -动态构建Json
EN

Stack Overflow用户
提问于 2019-12-08 20:33:06
回答 1查看 55关注 0票数 1

我试图动态构建我的json,但似乎无法在动态对象中获得结果来存储值:

代码语言:javascript
复制
    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;
    }

代码:

代码语言:javascript
复制
    var CD_Section = build.Build_CDSectionJson(sectionGUID, "", "sectioncon")

输出:

代码语言:javascript
复制
    {"section":{}}

我正在传递一些值,但它们是空的,我做错了什么?

我试过这个:

代码语言:javascript
复制
    dynamic cdSection = new JObject();
    cdSection.section.id = id;
    cdSection.section.style = style;
    cdSection.section.@class = @class;

但返回的结果为空。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-08 21:00:23

谢谢JLRishe

这样做效果很好:

代码语言:javascript
复制
    JObject cdSectionAttr = new JObject();
    cdSectionAttr.Add("id", id);
    cdSectionAttr.Add("style", style);
    cdSectionAttr.Add("@class", @class);

    JObject cdSection = new JObject();
    cdSection.Add("section", cdSectionAttr);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59235419

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档