为了账号安全,请及时绑定邮箱和手机立即绑定

将匿名对象转换为JSON对象

将匿名对象转换为JSON对象

C#
冉冉说 2021-04-09 16:22:45
抱歉,如果已经有人问过这个问题,假设我有一个匿名对象列表,如下所示:   Collection = new List<object>() {    new {        FirstSetting    =  new        {            FirstKEy = "Property vs Inspections",            color = "#FF6384",            fontStyle = "Arial",            sidePadding = 10,        }    },    new {        AnotherSettings =  new        {            text = "another text",            color = "#FF6384",            fontStyle = "Arial",            sidePadding = 10,        }    }};并希望使用转换为JSON字符串JsonConvert.SerializeObject(Collection, JsonSerializerSettings);我得到这个结果。[{    "FirstSetting": {        "FirstKey": "Property vs Inspections",        "color": "#FF6384",        "fontStyle": "Arial",        "sidePadding": 10    }}, {    "anotherSettings": {        "text": "another text",        "color": "#FF6384",        "fontStyle": "Arial",        "sidePadding": 10    }}]但我不希望有一个数组。我想让每个设置都是这样的对象。{    "FirstSetting": {        "FirstKey": "Property vs Inspections",        "color": "#FF6384",        "fontStyle": "Arial",        "sidePadding": 10    },    "anotherSettings": {        "text": "another text",        "color": "#FF6384",        "fontStyle": "Arial",        "sidePadding": 10    }}有人可以请问一下吗?
查看完整描述

3 回答

?
梵蒂冈之花

TA贡献1900条经验 获得超5个赞

为此,您需要以这种方式构造另一个匿名对象以获取所需的json。


如果您对集合进行序列化,那么显然它将转换为json中的array:


var obj = new

    {

        Result = new

        {

            FirstSetting = new

            {

                FirstKEy = "Property vs Inspections", color = "#FF6384", fontStyle = "Arial", sidePadding = 10

            }, 

            AnotherSettings = new

            {

                text = "another text", color = "#FF6384", fontStyle = "Arial", sidePadding = 10 

            }

        }

    };

生成的json将是:


{

   "Result":{

      "FirstSetting":{

         "FirstKey":"Property vs Inspections",

         "color":"#FF6384",

         "fontStyle":"Arial",

         "sidePadding":10

      },

      "anotherSettings":{

         "text":"another text",

         "color":"#FF6384",

         "fontStyle":"Arial",

         "sidePadding":10

      }

   }

}


查看完整回答
反对 回复 2021-04-17
?
波斯汪

TA贡献1811条经验 获得超4个赞

并不是那样会更好,但是如果有人发现它有用,那么您也可以为此使用字典。它仍然是干净的代码,并且从列表中删除了第一个对象“ Result”。我有点觉得它更干净,但这是首选。


var collection = new Dictionary<object, object>()

{

    ["FirstSetting"] = new

    {

        FirstKEy = "Property vs Inspections",

        color = "#FF6384",

        fontStyle = "Arial",

        sidePadding = 10,

    },

    ["AnotherSettings"] = new

    {

        text = "another text",

        color = "#FF6384",

        fontStyle = "Arial",

        sidePadding = 10,

    }

};


var jsonString = JsonConvert.SerializeObject(collection);

它输出到:


{

    "FirstSetting": 

    { 

        "FirstKEy":"Property vs Inspections",

        "color":"#FF6384",

        "fontStyle":"Arial",

        "sidePadding":10

    },

    "AnotherSettings":

    {

        "text":"another text",

        "color":"#FF6384",

        "fontStyle":"Arial",

        "sidePadding":10

    }

}


查看完整回答
反对 回复 2021-04-17
?
素胚勾勒不出你

TA贡献1827条经验 获得超9个赞

我认为,将其放在一个对象中会使它更具可读性。


Object mySettings = new {

    FirstSetting = new {

            FirstKEy = "Property vs Inspections",

            color = "#FF6384",

            fontStyle = "Arial",

            sidePadding = 10,

    },

    AnotherSettings = new {

        text = "another text",

        color = "#FF6384",

        fontStyle = "Arial",

        sidePadding = 10,

    }

};

以上可能会给您所需的结果。


查看完整回答
反对 回复 2021-04-17
  • 3 回答
  • 0 关注
  • 296 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信