将词典压缩到包含对象的最佳方法是什么?词典的键与顶级属性合并?
我有以下代码(我使用linqpad):
void Main()
{
Item item = new Item
{
Id = 1,
Description = "Item 1",
Attributes = new Dictionary<string, Object> {
{ "Attrib1", "Value1"},
{ "Attrib2", "Value2"}
}
};
string json = JsonConvert.SerializeObject(item, Newtonsoft.Json.Formatting.Indented);
json.Dump();
}
// Define other methods and classes here
class Item
{
public int Id { get; set; }
public String Description { get; set; }
public Dictionary<String, Object> Attributes { get; set; }
}
默认情况下,它被序列化为:
{
"Id": 1,
"Description": "Item 1",
"Attributes": {
"Attrib1": "Value1",
"Attrib2": "Value2"
}
}
我希望将其序列化为:
{
"Id": 1,
"Description": "Item 1",
"Attrib1": "Value1",
"Attrib2": "Value2"
}
其他假设:
有没有可以做到开箱即用的技巧,还是应该创建自定义转换器?
由于