我試圖用json.netJSON序列化格式
的對象序列化對象的集合看起來是這樣的:
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
}
}
很高興地考慮改變任何物體的形狀,或者甚至使用不同的序列化庫