Angular 2 Date deserialization

后端 未结 4 641
执念已碎
执念已碎 2020-12-05 07:27

I have an Angular 2 application. A service is requests data from an api that returns the results like the following:

{
    \"data\":[
        {\"id\":1,\"ti         


        
4条回答
  •  离开以前
    2020-12-05 07:53

    JSON does not provide any date specification, so it is entirely up to how it is serialized/deserialized.

    You could use reviver parameter of JSON.parse:

    this._http.get(this._getListUrl).map(res => JSON.parse(res.text(), this.reviver));
    
    reviver(key, value):any
    {
        if('timestamp' === key){
            //you can use any de-serialization algorithm here
            return new Date(value);
        }
        return value;
    }
    

提交回复
热议问题