You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

创建预约请求模型Web API:日期时间格式配置错误排查

解决预约请求类的JSON格式化问题

你遇到的问题根源在于:[DisplayFormat]和[DataType]这两个特性是给ASP.NET MVC视图引擎用的,完全不影响JSON的序列化/反序列化过程,所以才会出现格式不匹配的报错。下面针对主流的两种JSON序列化库,给出具体解决方案:

方案一:使用System.Text.Json(.NET Core 3.0+ 默认)

需要为日期和时间属性自定义JSON转换器,指定对应的格式:

using System;
using System.Text.Json;
using System.Text.Json.Serialization;

public class CreateAppointmentRequestModel
{
    // 把原来的字段改成自动属性,否则JSON序列化默认不会处理它
    public Guid Id { get; set; } = Guid.NewGuid();

    [JsonConverter(typeof(CustomDateConverter))]
    public DateTime Day { get; set; }

    [JsonConverter(typeof(CustomTimeConverter))]
    public DateTime StartTime { get; set; }

    public Guid CustomerId { get; set; }
}

// 处理dd/MM/yyyy格式的日期转换器
public class CustomDateConverter : JsonConverter<DateTime>
{
    private const string _dateFormat = "dd/MM/yyyy";

    public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        return DateTime.ParseExact(reader.GetString()!, _dateFormat, null);
    }

    public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
    {
        writer.WriteStringValue(value.ToString(_dateFormat));
    }
}

// 处理HH/mm格式的时间转换器
public class CustomTimeConverter : JsonConverter<DateTime>
{
    private const string _timeFormat = "HH/mm";

    public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        // 解析时用当天日期拼接成完整DateTime对象
        var timeStr = reader.GetString()!;
        return DateTime.ParseExact(timeStr, _timeFormat, null);
    }

    public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
    {
        writer.WriteStringValue(value.ToString(_timeFormat));
    }
}

方案二:使用Newtonsoft.Json(Json.NET)

如果你的项目依赖Json.NET,可以直接用它的特性指定格式,或者自定义转换器:

using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

public class CreateAppointmentRequestModel
{
    public Guid Id { get; set; } = Guid.NewGuid();

    // 指定日期格式为dd/MM/yyyy
    [JsonConverter(typeof(IsoDateTimeConverter), "dd/MM/yyyy")]
    public DateTime Day { get; set; }

    // 指定时间格式为HH/mm
    [JsonConverter(typeof(IsoDateTimeConverter), "HH/mm")]
    public DateTime StartTime { get; set; }

    public Guid CustomerId { get; set; }
}

// 如果需要更灵活的处理(比如异常捕获),也可以自定义转换器
public class CustomDateConverter : DateTimeConverterBase
{
    private const string _dateFormat = "dd/MM/yyyy";

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        return DateTime.ParseExact(reader.Value.ToString(), _dateFormat, null);
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        writer.WriteValue(((DateTime)value).ToString(_dateFormat));
    }
}

关键注意事项

  1. 你原来的Id是字段而非属性,JSON序列化默认只会处理公共属性,所以必须改成自动属性才能被正确序列化/反序列化。
  2. 反序列化时要确保前端传入的字符串格式严格匹配定义的格式(比如日期必须是dd/MM/yyyy,时间是HH/mm),否则会抛出解析异常。如果需要兼容宽松格式,可以在转换器里添加异常处理逻辑(比如尝试多个格式解析)。

内容的提问来源于stack exchange,提问作者Pikp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 02:00:43