gpt4 book ai didi

asp.net-core - ServiceStack - 年、月和日参数描述了即使使用 JsConfig 也无法表示的 DateTime

转载 作者:行者123 更新时间:2023-12-04 11:02:21 26 4
gpt4 key购买 nike

我在网上查看了多种资源,但无论我尝试什么,似乎都不起作用。我究竟做错了什么?

使用指令

using ServiceStack;
using ServiceStack.Text;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;

代码
JsConfig<DateTime>.DeSerializeFn = str => DateTime.ParseExact(str, "dd/MM/yyyy hh:mm", CultureInfo.InvariantCulture);

using (var fs = File.Open(@"C:\temp\data.csv", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
var data = CsvSerializer.DeserializeFromStream<List<Class>>(fs);
}

也试过
JsConfig<DateTime>.DeSerializeFn = str => DateTime.ParseExact(str, "dd/MM/yyyy hh:mm", CultureInfo.InvariantCulture);
var list = File.ReadAllText(@"C:\temp\data.csv").FromCsv<List<Class>>();

什么是 csv 文件?

开始日期、首次兑换日期、首次订阅日期

01/08/2014 00:00, 31/08/2014 00:00, 01/08/2014 00:00

30/09/2014 00:00, 01/08/2015 00:00, 30/09/2013 00:00

最佳答案

问题是因为您的 CSV 有两个 ,分隔符和 空格分隔符,当使用 , 时分隔符 CSV 对空格敏感,例如你的标题应该是:

StartDate,FirstRedemptionDate,FirstSubscriptionDate

作为当前 'FirstRedemptionDate' 的 header 值是 ' FirstRedemptionDate' .

我进行了更改,以便在 this commit 中自动修剪标题它允许您的自定义 DateTime 反序列化器与带有前导/尾随空格的 header 匹配,可从 latest v5.7.1 that's now on MyGet 获得.

因为您的日期值中还有前导空格,所以您还需要在解析之前修剪您的值,例如:
JsConfig<DateTime>.DeSerializeFn = str => 
DateTime.ParseExact(str.Trim(), "dd/MM/yyyy hh:mm", CultureInfo.InvariantCulture);

现在应该使用最新的 v5.7.1 of ServiceStack.Text on MyGet 正确解析.

关于asp.net-core - ServiceStack - 年、月和日参数描述了即使使用 JsConfig 也无法表示的 DateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58725752/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com