我试图用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
}
}
很高兴地考虑改变任何物体的形状,或者甚至使用不同的序列化库