how to force netwtonsoft json serializer to serialize datetime property to string?

前端 未结 1 1849
慢半拍i
慢半拍i 2020-12-13 06:39

I am using Newtonsoft\'s Json when i serialze a date time property i get the json response as:

...\"CreatedOn\":\"\\/Date(1317303882420+0500)\\/\",...


        
相关标签:
1条回答
  • 2020-12-13 06:53

    From a post made by James Newton-King on StackOverflow, it looks like you can do this.

    string isoJson = JsonConvert.SerializeObject(this, new IsoDateTimeConverter());
    // {"Details":"Application started.","LogDate":"2009-02-15T00:00:00Z"}    
    

    Referenced answer: Parsing JSON DateTime from Newtonsoft's JSON Serializer

    Also here is the documentation on Json.NET and dates: Serializing Dates in JSON

    Here is an example of using the DateTimeFormat property to customize the output:

    return JsonConvert.SerializeObject(this, Formatting.None, new IsoDateTimeConverter() { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" });
    
    0 讨论(0)
提交回复
热议问题