- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在阅读这篇关于日期时间相关格式支持的 MSDocs 文章
https://docs.microsoft.com/en-us/dotnet/standard/datetime/system-text-json-support#support-for-the-iso-8601-12019-format
我只是想弄清楚默认配置没有按预期工作,或者我一定遗漏了一些东西。
我的意思是:
DateTimeOffset.Parse("2021-03-17T12:03:14+0000");
工作得很好。
JsonSerializer.Deserialize<TestType>(@"{ ""CreationDate"": ""2021-03-17T12:03:14+0000"" }");
没有。
using System;
using System.Text.Json;
using System.Threading.Tasks;
namespace CSharpPlayground
{
public record TestType(DateTimeOffset CreationDate);
public static class Program
{
public static void Main()
{
var dto = DateTimeOffset.Parse("2021-03-17T12:03:14+0000");
Console.WriteLine(dto);
var testType = JsonSerializer.Deserialize<TestType>(@"{ ""CreationDate"": ""2021-03-17T12:03:14+0000"" }");
Console.WriteLine(testType.CreationDate);
}
}
}
抛出以下异常:
System.Text.Json.JsonException: The JSON value could not be converted to CSharpPlayground.TestType. Path: $.CreationDate | LineNumber: 0 | BytePositionInLine: 44.
---> System.FormatException: The JSON value is not in a supported DateTimeOffset format.
最佳答案
使用 System.Text.Json
实现自定义 JSON 行为通常使用自定义转换器完成。不幸的是,对于不同的日期格式并没有提供很多开箱即用的工具,因此如果您需要反序列化默认设置中没有的内容 ISO 8601-1:2019
格式你需要自己滚动。
幸运的是,这不是很困难:
using System.Text.Json;
using System.Text.Json.Serialization;
public class DateTimeOffsetConverterUsingDateTimeParse : JsonConverter<DateTimeOffset >
{
public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
Debug.Assert(typeToConvert == typeof(DateTimeOffset));
return DateTimeOffset .Parse(reader.GetString());
}
public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}
}
通过这种方式,您将获得与使用
DateTimeOffset.Parse()
时相同的行为。 .
public record TestType(DateTimeOffset CreationDate);
public class Program
{
public static void Main(string[] args)
{
JsonSerializerOptions options = new JsonSerializerOptions();
options.Converters.Add(new DateTimeOffsetConverterUsingDateTimeParse());
var testType = JsonSerializer.Deserialize<TestType>(@"{ ""CreationDate"": ""2021-03-17T12:03:14+0000"" }",options);
Console.WriteLine(testType.CreationDate);
}
}
关于c# - System.FormatException : The JSON value is not in a supported DateTimeOffset format,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67857022/
为什么这个指数在使用 Decimal.Parse 转换为小数时会抛出 FormatException。 String stringValue = "8.83080183680678E-05"; Dec
每当我尝试在数据库中插入日期时,我都会收到 formatException。 我将日期转换为字符串并尝试插入到数据库中。我已经尝试了所有方法,但它被捕获为异常。找到下面的代码: $ DateTime
在我的 SQL Server Management Studio 中,我创建的其中一个表 (ACTION_LOCATION_MAP) 具有以下设计: 在我的一个函数中,我正在调用一个存储过程来检索所有
我收到 FormatException,我不知道为什么。 System.FormatException was unhandled by user code Message=Input strin
所以我还在学习C#,我有一点问题。我正在制作一个非常简单的 Windows 窗体应用程序,有两个文本框,输入和输出,用于从十六进制到十进制的转换。这是我的代码: string inpu
此行抛出 FormatException: var dataString = string.Format("email_address:{0}, status:subscribed, merge_fi
我的代码是 objVehicle.TaxAmount = CalculateTax_TaxableVeh( objVehicle.IsLogging, Convert.ToChar
我已经在座位上坐了一个多小时了,不知道出了什么问题。有人可以帮忙吗? 错误据说是“将字符串转换为 DateTime 时,在将每个变量放入 DateTime 对象之前解析字符串以获取日期。 字段名称“L
我正在使用 C# Windows 应用程序 2010 Express。这里我有 3 个文本框,用于显示总费用、支付金额、应付金额。总费用应从 DB 获取,支付金额应由用户输入,应付金额应由系统计算。这
从 Unity 2018.1.something 切换到 2018.2.0.b3 后,我无法再为 Android 构建,我得到: FormatException: Input string was n
我正在使用 visual studio 2008 开发适用于 windows CE 6.0 的紧凑型框架软件。 我有这个“奇怪的?” isNumeric 方法有问题。还有其他更好的方法来完成这项工作吗
在此 DatePicker 中,如果我输入无效日期,例如 20001 年 1 月 1 日(输入键) 我得到以下异常 A first chance exception of type 'System.F
有人帮我解决了另一个问题,并给了我一个非常有效的代码示例。我尝试稍微调整一下以写入文件而不是控制台,但出现以下错误: System.FormatException: String 未被识别为有效的 D
我创建了一个包含一些列的 DataGridView。顺序栏只允许用户输入整数。当我输入“j”(例如)并尝试添加 try catch 来解决问题时它抛出 FormatException,但它看起来不起作
这个问题在这里已经有了答案: Why does TimeSpan.ParseExact not work (6 个答案) 关闭 9 年前。 我想从格式为“MM:SS”的字符串中提取总秒数。例如:01
我创建了一个程序,并对其进行了广泛的测试,我收到一条错误消息,提示“FormatException 未处理,输入字符串的格式不正确”。当我将任一文本框留空并按下“完成”按钮时会出现问题,但如果我输入低
我在尝试修复简单的 C# 代码时遇到了问题。这是代码: using System; using System.Collections.Generic; using System.Linq; using
我正在为 iPad 应用程序制作一些服务,以使用广告发送数据到 CRM Dynamics。在这个过程中,我遇到了这个奇怪但又令人着迷的问题。 我必须将 Guid 分配给一些尚未分配给其他实体的记录。因
这个问题在这里已经有了答案: How to escape braces (curly brackets) in a format string in .NET (12 个答案) 关闭 9 年前。 我
我遇到的问题尤其与 CSVHelper 库有关。 我的 csv 文件看起来像这样: Number,Date,Account,Amount,Subcategory,Memo ,09/05/2017,X
我是一名优秀的程序员,十分优秀!