How do I format a Microsoft JSON date?

后端 未结 30 3079
伪装坚强ぢ
伪装坚强ぢ 2020-11-21 04:48

I\'m taking my first crack at Ajax with jQuery. I\'m getting my data onto my page, but I\'m having some trouble with the JSON data that is returned for Date data types. Basi

30条回答
  •  难免孤独
    2020-11-21 05:03

    I ended up adding the "characters into Panos's regular expression to get rid of the ones generated by the Microsoft serializer for when writing objects into an inline script:

    So if you have a property in your C# code-behind that's something like

    protected string JsonObject { get { return jsSerialiser.Serialize(_myObject); }}
    

    And in your aspx you have

    
    

    You'd get something like

    var myObject = '{"StartDate":"\/Date(1255131630400)\/"}';
    

    Notice the double quotes.

    To get this into a form that eval will correctly deserialize, I used:

    myObject = myObject.replace(/"\/Date\((\d+)\)\/"/g, 'new Date($1)');
    

    I use Prototype and to use it I added

    String.prototype.evalJSONWithDates = function() {
        var jsonWithDates = this.replace(/"\/Date\((\d+)\)\/"/g, 'new Date($1)');
        return jsonWithDates.evalJSON(true);
    }
    

提交回复
热议问题