`Newtonsoft.json` specify a property using a specific time format

newtonsoft.json Specify a property using a specific time format

Intro

newtonsoft.json.NET is the next most popular JSON manipulation library, originally JSON.Netlater renamed Newtonsoft.Json, until we recommend the use, in addition to good performance, the main feature-rich, basically meet all of the possible use scenarios (not case lower case, and now No ,,).

Encountered such a demand, the global use of a time format, certain properties using special time format, here for a date, for example

Solution

The solution: a custom Converter, a property for a certain use, DateTimeFormatConverter Source :

using Newtonsoft.Json.Converters;

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

Set Converter on the need to set the format properties 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 request address https://reservation.weihanli.xyz/api/Reservation?pageNumber=1&pageSize=5 , the returned data shown below:

{
    "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
}

You can see ReservationForDatethe format returned after serialization format as our designated a ~

Guess you like

Origin www.cnblogs.com/weihanli/p/11080531.html