IsoDateTimeConverter忽略我指定的日期格式

时间:2019-07-03 12:33:26

标签: c# .net json json.net

在序列化过程中,序列化程序会忽略我指定的日期格式,并从本地pc中获取它,例如,我指定了“ dd / MM / yyyy”,而本地pc格式为“ dd-mmm-yyyy”,序列化程序从pc中获取格式,谁能帮我?为什么它来自本地PC?

波纹管都是例子

class Program
    {
        static void Main(string[] args)
        {

            var obj = new Request { Id = "1", Language = "en", BirthDate = new DateTime(2012, 12, 31) };
            var json = JsonConvert.SerializeObject(obj);
        }
     }




    public class Request
    {
        [JsonProperty(PropertyName = "player")]
        public string Id { get; set; } //id

        [JsonProperty(PropertyName = "lang")]
        public string Language { get; set; }

        [JsonProperty(PropertyName = "birthdate")]
        [JsonConverter(typeof(JsonConverters.DateTimeConverter), "dd/MM/yyyy")]
        public DateTime? BirthDate { get; set; } //birthdate
    }

    public class JsonConverters
    {
        public class DateTimeConverter : IsoDateTimeConverter
        {
            public DateTimeConverter(string format)
            {
                DateTimeFormat = format;
            }
        }
    }

序列化结果是

{
 "player": "1",
 "lang":"en",
 "birthdate":"31-12-2012"
}

谢谢

0 个答案:

没有答案