1. 程式人生 > >`newtonsoft.json` 指定某個屬性使用特定的時間格式

`newtonsoft.json` 指定某個屬性使用特定的時間格式

newtonsoft.json 指定某個屬性使用特定的時間格式

Intro

newtonsoft.json 是 .NET 下最受歡迎 JSON 操作庫,原為 JSON.Net 後改名為 Newtonsoft.Json,之前一直推薦大家使用,除了效能好之外,主要是功能豐富,基本滿足所有的可能用到的場景(不區分小寫,現在還不行,,)。

遇到這樣一個需求,全域性使用一種時間格式,某些屬性使用特殊的時間格式,這裡以一個日期為例

Solution

解決辦法:自定義一個 Converter,針對某一個屬性使用,DateTimeFormatConverter原始碼:

using Newtonsoft.Json.Converters;

namespace WeihanLi.Common.Json
{
  public class DateTimeFormatConverter : IsoDateTimeConverter
  {
    public DateTimeFormatConverter(string format)
    {
      DateTimeFormat = format;
    }
  }
}

在需要設定格式的屬性上設定 Converter https://github.com/WeihanLi/ActivityReservation/blob/dev/ActivityReservation.Helper/ViewModels/ReservationViewModel.cs#L8

[Display(Name = "預約日期")]
[JsonConverter(typeof(DateTimeFormatConverter), "yyyy-MM-dd")]
public DateTime ReservationForDate { get; set; }

請求 api 地址 https://reservation.weihanli.xyz/api/Reservation?pageNumber=1&pageSize=5,返回的資料如下所示:

{
    "Data": [
        {
            "ReservationForDate": "2019-06-10",
            "ReservationForTime": "08:00~09:50",
            "ReservationPersonPhone": "123****0112",
            "ReservationPersonName": "兒**",
            "ReservationUnit": "51",
            "ReservationPlaceName": "多媒體工作室",
            "ReservationActivityContent": "62",
            "ReservationId": "f7ab9128-0977-4fd8-9b1a-92648228b397",
            "ReservationTime": "2019-06-09 05:19:11",
            "ReservationStatus": 1
        },
        {
            "ReservationForDate": "2019-06-12",
            "ReservationForTime": "10:00-12:00",
            "ReservationPersonPhone": "133****3541",
            "ReservationPersonName": "試**",
            "ReservationUnit": "ss",
            "ReservationPlaceName": "多媒體工作室",
            "ReservationActivityContent": "ss",
            "ReservationId": "6c145aea-dc14-4ed9-a47f-48c0b79f7601",
            "ReservationTime": "2019-06-11 12:45:14",
            "ReservationStatus": 0
        },
        {
            "ReservationForDate": "2019-06-17",
            "ReservationForTime": "14:00-16:00",
            "ReservationPersonPhone": "138****3883",
            "ReservationPersonName": "大**",
            "ReservationUnit": "1",
            "ReservationPlaceName": "多媒體工作室",
            "ReservationActivityContent": "1",
            "ReservationId": "cebea7bf-44b1-4565-8cdd-78b6156c5f4d",
            "ReservationTime": "2019-06-10 02:52:18",
            "ReservationStatus": 1
        },
        {
            "ReservationForDate": "2019-06-17",
            "ReservationForTime": "08:00-10:00",
            "ReservationPersonPhone": "132****4545",
            "ReservationPersonName": "冷**",
            "ReservationUnit": "技術部",
            "ReservationPlaceName": "多媒體工作室",
            "ReservationActivityContent": "技術部培訓",
            "ReservationId": "07f6f8fd-f232-478e-9a94-de0f5fa9b4e9",
            "ReservationTime": "2019-06-10 01:44:52",
            "ReservationStatus": 2
        },
        {
            "ReservationForDate": "2019-06-22",
            "ReservationForTime": "10:00~11:50",
            "ReservationPersonPhone": "132****3333",
            "ReservationPersonName": "測**",
            "ReservationUnit": "測試",
            "ReservationPlaceName": "多媒體工作室",
            "ReservationActivityContent": "測試",
            "ReservationId": "27d0fb7a-ce14-4958-8636-dd10e5526083",
            "ReservationTime": "2019-06-18 10:57:06",
            "ReservationStatus": 1
        }
    ],
    "PageNumber": 1,
    "PageSize": 5,
    "TotalCount": 18,
    "PageCount": 4,
    "Count": 5
}

可以看到 ReservationForDate 序列化之後返回的格式如我們指定的格式