简体   繁体   中英

NewtonSoft JsonConvert.SerializeObject, \" added after serializing DateTime

After serializing a datetime property with NewtonSoft, the value went from 2021-02-19 10:00 as DateTime to \\"2021-02-19T10:00:00\\" as string. Deserializing failed without replacing those \\" . This replace was a quick fix, but now am I curious how this is possible and how I can avoid the replace.

When things are missing in my post, please give me a comment.

Instead of serializing a DateTime with JsonConvert , you can use one of the standard format strings. The equivalent format to the one the Serialize method produces is the sortable specifier, s . So:

new DateTime(2021, 2, 19, 10, 0, 0).ToString("s")

produces:

2021-02-19T10:00:00

without the quotes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM