Javascript JSON Date Deserialization

前端 未结 5 1950
轻奢々
轻奢々 2020-12-16 15:01

I am trying to deserialize a json object that has a javascript date in it. When JSON.stringify is called on the object, dates are serialized to strings that are not properl

5条回答
  •  别那么骄傲
    2020-12-16 15:24

    The JSON spec does not include special formatting for dates. As such they are often serialized as a string, sometimes with special markings to indicate it should be treated as a Date object if the language supports them. As such, most (all?) browser-native JSON parsers can not round-trip a Date object properly.

    There are several good libraries that help with this - I very much like MomentJS though I have used datejs in the past as well. You would just need to iterate over your objects and convert the proper fields to Date objects after they have been parsed.

    I find it helpful to remember that the JSON format is much more restrictive than JavaScript object literal notation.

提交回复
热议问题