关于The JSON value could not be converted to System.DateTime的解决方案
如下json格式提交到后台后报:
The JSON value could not be converted to System.DateTime. Path: $.beginTime | LineNumber: 3 | BytePositionInLine: 33.{
"beginTime": "2023-06-08T08:00:00"
}造成这个错误的原因为程序无法正常解析该json, 主要是为了提升执行效率;System.Text.Json作为微软内置json处理,效率更高更快。那么这个微软官方的json会认识什么格式的时间值呢?它只认下面的格式:{
"beginTime": "2023-06-08T08:00:00"
}年月日和时分秒之间加一个T就OK了- 若你执意不想要这个T,我们可以将微软默认的Json解析换掉,换成NewtonsoftJson就可以了。
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers().AddNewtonsoftJson();
} - 当然如果是属性的方式,那么以上方式较麻烦,可以通过重写JsonConverter,实现属性上的正确解析
/// <summary>
/// 识别日期格式为:yyyy-MM-dd HH:mm:ss的json字符串
/// </summary>
public class DateTimeISO8601NoT : JsonConverter<DateTime>
{
private const string DateTimeFormat = "yyyy-MM-dd HH:mm:ss"; public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String && DateTime.TryParseExact(reader.GetString(), DateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime dateTime))
{
return dateTime;
} throw new JsonException($"Unable to convert '{reader.GetString()}' to {typeof(DateTime)}.");
} public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString(DateTimeFormat));
}
} 然后为属性打上:
[JsonConverter(typeof(DateTimeISO8601NoT))]
public DateTime BeginTime { get; set; }
关于The JSON value could not be converted to System.DateTime的解决方案的更多相关文章
- 在mvc返回JSON时出错:序列化类型为“System.Data.Entity.DynamicProxies.Photos....这个会的对象时检测到循环引用 的解决办法
在MVC中返回JSON时出错,序列化类型为“System.Data.Entity.DynamicProxies.Photos....这个会的对象时检测到循环引用. public ActionResul ...
- net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案
net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案 今天在用List集合转换成json数组的时候发生了这个错误,这个 ...
- json.dumps错误:'utf8' codec can't decode byte解决方案-乾颐堂
一次在使用json.dumps()过程中,出现错误提示: ERROR:"UnicodeDecodeError: 'utf8' codec can't decode byte 0xe1 in ...
- json.dumps错误:'utf8' codec can't decode byte解决方案
一次在使用json.dumps()过程中,出现错误提示: ERROR:"UnicodeDecodeError: 'utf8' codec can't decode byte 0xe1 in ...
- json-lib-2.4-jdk15.jar 报错 net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案(Hibernate)
使用hibernate容易出现该问题,主要是由于pojo类属性存在级联关系.比如说员工和部门,在员工表里面有部门属性,而在部门表里面有个员工集合,这样就存在了嵌套引用的问题了,就会抛出这个异常. 解决 ...
- NewtonSoft对象转json时,把 NULL 转 "" , 过滤 NULL, DateTime 时间类型去除 T
IsoDateTimeConverter timeConverter = new IsoDateTimeConverter(); timeConverter.DateTimeFormat = &quo ...
- .NET Core 3.0 System.Text.Json 和 Newtonsoft.Json 行为不一致问题及解决办法
行为不一致 .NET Core 3.0 新出了个内置的 JSON 库, 全名叫做尼古拉斯 System.Text.Json - 性能更高占用内存更少这都不是事... 对我来说, 很多或大或小的项目能少 ...
- Web API <五> 序列化
在 Asp.Net Web Api 中提供了两种 媒体类型格式化器(mime-type formatter),分别用于支持 JSON 和 XML 数据的格式化处理.默认两种格式化器已集成到了 Asp. ...
- .Net Core 2.2升级3.1的避坑指南
写在前面 微软在更新.Net Core版本的时候,动作往往很大,使得每次更新版本的时候都得小心翼翼,坑实在是太多.往往是悄咪咪的移除了某项功能或者组件,或者不在支持XX方法,这就很花时间去找回需要的东 ...
- 理解ASP.NET Core - 模型绑定&验证(Model Binding and Validation)
注:本文隶属于<理解ASP.NET Core>系列文章,请查看置顶博客或点击此处查看全文目录 模型绑定 什么是模型绑定?简单说就是将HTTP请求参数绑定到程序方法入参上,该变量可以是简单类 ...
随机推荐
- FANUC发那科机器人主板维修,故障问题检测
电容损坏引发的故障在电子设备中是特别高的,其中尤其以电解电容的损坏为常见 电容损坏表现为:1.容量变小:2.完全失去容量:3.漏电:4.短路. 电容在电路中所起的作用不同,引起的故障也各有特点.在发那 ...
- 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
#div_digg { float: right; font-size: 12px; margin: 10px; text-align: center; width: 120px; position: ...
- JS代码执行
- mysql安装以及2059 - Authentication plugin 'caching_sha2_password' cannot be loaded:报错的解决办法
2059 - Authentication plugin 'caching_sha2_password' cannot be loaded: dlopen(../Frameworks/caching_ ...
- 基于webman实现的服务层框架-webman-biz-framework
简介 webman的基础上扩展的一个服务层框架,基于分层体系结构的代码模式. 如果觉得有用,可以帮我在webman-biz-framework点个小星星哟,也希望大家交流 分层体系结构的代码模式 什么 ...
- hotmail 获取邮箱授权码
第一步:登录microsoft账户,进入到安全性页面. https://account.microsoft.com/?lang=zh-CN&refd=account.live.com& ...
- 在 Mac/Windows 系统中使用 Laradock 搭建基于 Docker 的 Laravel 开发环境
Laradock 是 Docker 提供的完整 PHP 本地开发环境的一个镜像,任何环境下都可以安装 Docker来运行此开发环境,墙裂安利 简介 Laradock 是为 Docker 提供的完整 P ...
- IvorySQL 4.0 发布:全面支持 PostgreSQL 17
2024 年 12 月 24 日,IvorySQL 4.0 发布了! 这是 2024 年以来发布的最新版本,相较于上个版本,有了较大的变化. 该版本有多项增强:全面支持 PostgreSQL 17,新 ...
- linux 下给网卡添加ipv6、路由
route命令的使用举例如下: route // 显示路由信息. route add –host 192.168.1.110 dev eth0 // 给网卡eth0的路由表中加入新地址192 ...
- [每日算法 - 华为机试] 剑指 Offer 57 - II. 和为s的连续正数序列 「滑动窗口」
入口 力扣https://leetcode.cn/problems/he-wei-sde-lian-xu-zheng-shu-xu-lie-lcof/ 题目描述 输入一个正整数 target ,输出所 ...