【发布时间】:2014-10-13 22:26:39
【问题描述】:
我正在使用 ASP.NET Web API。我有以下代码,responseIEnumerable<StripeInvoice> 中的日期在序列化为 JSON 时没有被转换为 ISO 8601 格式。它们采用 Microsoft .NET 格式,例如 /Date(1412657602)/。使用 IsoDateTimeConverter() 应转换为 ISO 8601 格式,例如 2011-07-14T19:43:37+0100。为什么这不适用于IEnumerable<StripeInvoice>?
IEnumerable<StripeInvoice> response = service.List();
string json = JsonConvert.SerializeObject(response, new IsoDateTimeConverter());
编辑
这是来自我的 Web API 控制器的完整方法。
public IEnumerable<StripeInvoice> GetUserInvoices(string id)
{
var options = new StripeInvoiceListOptions()
{
CustomerId = id
};
// Using https://github.com/jaymedavis/stripe.net stripe library
var invoiceService = new StripeInvoiceService();
invoiceService.ApiKey = "key";
IEnumerable<StripeInvoice> response = invoiceService.List(options);
// test
IEnumerable<SomeObject> objs = new SomeObject[] {
new SomeObject() { SomeDate = DateTime.Now.AddDays(-1) },
new SomeObject() { SomeDate = DateTime.Now }
};
// return objs; // this serialized the dates properly into ISO 8601
return response; // this doesn't serialize to ISO 8601. It remains in /Date(142657602)/
}
【问题讨论】:
-
你能提供一个完整的例子来展示这个问题吗?我似乎无法重现您所看到的:dotnetfiddle.net/xus9wM
-
@AndrewWhitaker 您的 dotnetfiddle
objs在 Web API 控制器中工作。我在控制器中使用的 IEnumerable 仍然没有序列化为 ISO 8601。我不知道有什么区别。我将使用我正在使用的更新代码进行编辑。顺便说一句,我正在从名为 Stripe 的支付服务中获取我的IEnumerable数据。 stripe.com
标签: asp.net json asp.net-web-api json.net stripe.net