为什么这个JSON不会反序列化回序列化的模型?它将反序列化为Bar列表,但不反序列化为实现Bar列表的对象。
public class Bars : List<Bar>
{
}
public class Bar
{
public decimal Open { get; set; }
public decimal High { get; set; }
public decimal Low { get; set; }
public decimal Close { get; set; }
}
private void test()
{
Bars bars = new Bars()
{
new Bar(){Open = 1.5269M, High = 1.6001M, Low = 1.4012M, Close = 1.5277M},
new Bar(){Open = 1.5277M, High = 1.6003M, Low = 1.5276M, Close = 1.7008M},
new Bar(){Open = 1.7008M, High = 1.9003M, Low = 1.4276M, Close = 1.60098M}
};
JavaScriptSerializer jss = new JavaScriptSerializer();
string json = jss.Serialize(bars);
//works
var dsBars1 = jss.Deserialize<List<Bar>>(json);
//fails
var dsBars2 = jss.Deserialize<Bars>(json);
}
答案 0 :(得分:1)
您可以使用Json.Net。下面的代码工作......
var json = JsonConvert.SerializeObject(bars);
var dsBars1 = JsonConvert.DeserializeObject<List<Bar>>(json);
var dsBars2 = JsonConvert.DeserializeObject<Bars>(json);