将匿名对象转换为JSON对象

抱歉,如果已经有人问过这个问题,假设我有一个匿名对象列表,如下所示:


   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

    }

}

有人可以请问一下吗?


冉冉说
浏览 297回答 3
3回答

梵蒂冈之花

为此,您需要以这种方式构造另一个匿名对象以获取所需的json。如果您对集合进行序列化,那么显然它将转换为json中的array:var obj = new&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Result = new&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FirstSetting = new&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FirstKEy = "Property vs Inspections", color = "#FF6384", fontStyle = "Arial", sidePadding = 10&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AnotherSettings = new&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text = "another text", color = "#FF6384", fontStyle = "Arial", sidePadding = 10&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };生成的json将是:{&nbsp; &nbsp;"Result":{&nbsp; &nbsp; &nbsp; "FirstSetting":{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"FirstKey":"Property vs Inspections",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"color":"#FF6384",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"fontStyle":"Arial",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"sidePadding":10&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; "anotherSettings":{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"text":"another text",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"color":"#FF6384",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"fontStyle":"Arial",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"sidePadding":10&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;}}

波斯汪

并不是那样会更好,但是如果有人发现它有用,那么您也可以为此使用字典。它仍然是干净的代码,并且从列表中删除了第一个对象“ Result”。我有点觉得它更干净,但这是首选。var collection = new Dictionary<object, object>(){&nbsp; &nbsp; ["FirstSetting"] = new&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; FirstKEy = "Property vs Inspections",&nbsp; &nbsp; &nbsp; &nbsp; color = "#FF6384",&nbsp; &nbsp; &nbsp; &nbsp; fontStyle = "Arial",&nbsp; &nbsp; &nbsp; &nbsp; sidePadding = 10,&nbsp; &nbsp; },&nbsp; &nbsp; ["AnotherSettings"] = new&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; text = "another text",&nbsp; &nbsp; &nbsp; &nbsp; color = "#FF6384",&nbsp; &nbsp; &nbsp; &nbsp; fontStyle = "Arial",&nbsp; &nbsp; &nbsp; &nbsp; sidePadding = 10,&nbsp; &nbsp; }};var jsonString = JsonConvert.SerializeObject(collection);它输出到:{&nbsp; &nbsp; "FirstSetting":&nbsp;&nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "FirstKEy":"Property vs Inspections",&nbsp; &nbsp; &nbsp; &nbsp; "color":"#FF6384",&nbsp; &nbsp; &nbsp; &nbsp; "fontStyle":"Arial",&nbsp; &nbsp; &nbsp; &nbsp; "sidePadding":10&nbsp; &nbsp; },&nbsp; &nbsp; "AnotherSettings":&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "text":"another text",&nbsp; &nbsp; &nbsp; &nbsp; "color":"#FF6384",&nbsp; &nbsp; &nbsp; &nbsp; "fontStyle":"Arial",&nbsp; &nbsp; &nbsp; &nbsp; "sidePadding":10&nbsp; &nbsp; }}

素胚勾勒不出你

我认为,将其放在一个对象中会使它更具可读性。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,    }};以上可能会给您所需的结果。
打开App,查看更多内容
随时随地看视频慕课网APP