第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將匿名對象轉(zhuǎn)換為JSON對象

將匿名對象轉(zhuǎn)換為JSON對象

C#
冉冉說 2021-04-09 16:22:45
抱歉,如果已經(jīng)有人問過這個問題,假設(shè)我有一個匿名對象列表,如下所示:   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,        }    }};并希望使用轉(zhuǎn)換為JSON字符串JsonConvert.SerializeObject(Collection, JsonSerializerSettings);我得到這個結(jié)果。[{    "FirstSetting": {        "FirstKey": "Property vs Inspections",        "color": "#FF6384",        "fontStyle": "Arial",        "sidePadding": 10    }}, {    "anotherSettings": {        "text": "another text",        "color": "#FF6384",        "fontStyle": "Arial",        "sidePadding": 10    }}]但我不希望有一個數(shù)組。我想讓每個設(shè)置都是這樣的對象。{    "FirstSetting": {        "FirstKey": "Property vs Inspections",        "color": "#FF6384",        "fontStyle": "Arial",        "sidePadding": 10    },    "anotherSettings": {        "text": "another text",        "color": "#FF6384",        "fontStyle": "Arial",        "sidePadding": 10    }}有人可以請問一下嗎?
查看完整描述

3 回答

?
梵蒂岡之花

TA貢獻1900條經(jīng)驗 獲得超5個贊

為此,您需要以這種方式構(gòu)造另一個匿名對象以獲取所需的json。


如果您對集合進行序列化,那么顯然它將轉(zhuǎn)換為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

      }

   }

}


查看完整回答
反對 回復(fù) 2021-04-17
?
波斯汪

TA貢獻1811條經(jīng)驗 獲得超4個贊

并不是那樣會更好,但是如果有人發(fā)現(xiàn)它有用,那么您也可以為此使用字典。它仍然是干凈的代碼,并且從列表中刪除了第一個對象“ 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

    }

}


查看完整回答
反對 回復(fù) 2021-04-17
?
素胚勾勒不出你

TA貢獻1827條經(jīng)驗 獲得超9個贊

我認(rèn)為,將其放在一個對象中會使它更具可讀性。


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,

? ? }

};

以上可能會給您所需的結(jié)果。


查看完整回答
反對 回復(fù) 2021-04-17
  • 3 回答
  • 0 關(guān)注
  • 311 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號