【发布时间】:2016-10-25 11:45:06
【问题描述】:
Angular 在很多地方都需要 Date 对象,而 JSON 包含日期的字符串表示形式。
我想添加一个包含日期值的属性数组:
class Foo
{
public int IntProp {get;set;}
public DateTime? Prop1 {get;set;}
public DateTime Prop2 {get;set;}
public Bar Bar {set;set;}
}
class Bar
{
public DateTime Prop {get;set;}
public IEnumerable<DateTime?> Dates {get;set;}
}
Foo 应该像这样被序列化:
{
"IntProp": 1,
"Prop1": "...",
"Prop2": "...",
"Bar": {
"Prop": "..."
},
"<Dates>": [ "Prop1", "Prop2", "Bar.Prop", "Bar.Dates"]
}
这使我可以在客户端自动将字符串转换为日期对象,而无需测试每个属性是否可转换为Date,就像在此question 中描述的那样。
我可以收集日期属性的路径,但不知道如何将填充数组添加到根。
【问题讨论】:
-
这里可能有点xy problem。 Json.NET 可以发出多种格式的日期,请参阅here。您希望
DateTIme属性的 JSON 在不需要额外的"<Dates>"属性的情况下看起来如何? -
@dbc 我更喜欢
Date。但我不想离开 ISO 8601。Date没有特殊的专用格式。我们可以区分字符串、数字、数组和对象,但是日期被格式化为字符串。
标签: angularjs json datetime json.net iso8601