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
}
}
}
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
}
}
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,
}
};
以上可能会给您所需的结果。
- 3 回答
- 0 关注
- 296 浏览
添加回答
举报