我正在尝试使用 json.net 序列化一组对象
该对象如下所示:
public class TaxBand
{
public TaxBand(string county, decimal taxPercentage)
{
County = county;
Tax = taxPercentage;
}
public string County { get; private set; }
public decimal Tax { get; private set; }
}
它们包含在这样的结构中:
var data = new Dictionary<string, List<TaxBand>>();
data = PopulateDataset();
string json = JsonConvert.SerializeObject(data, Formatting.Indented);
这会产生如下所示的 json:
{
"North": [
{
"County": "Merseyside",
"Tax": 5.0
},
{
"County": "Greater Manchester",
"Tax": 6.0
}
],
"South": [
{
"County": "Greater London",
"Tax": 5.5
},
{
"County": "Surry",
"Tax": 6.2
}
]
}
是否可以生成如下所示的 json:
{
"North":
{
"Merseyside": 5.0,
"Greater Manchester" : 6.0
},
"South":
{
"Greater London": 5.5,
"Surry": 6.2
}
}
很高兴考虑更改任何对象的形状,或者确实使用不同的序列化库