views:

39

answers:

2

After much head scratching as to why my returned Json string is breaking JSON.parse, I have realized that it is the returned dates that it doesn't like.

.net property:-

    [JsonProperty("start")]
    [JsonConverter(typeof(JavaScriptDateTimeConverter))]
    public DateTime Start
    {
        get { return _start; }
        set { _start = value; }
    }

Output Json String from web service:-

"{\"id\":9815,\"start\":new Date(1286535600000),\"end\":new Date(1286537400000),\"title\":\"Title of meeting\",\"owner\":\"D\",\"contactdetails\":\"David\",\"room\":{\"title\":\"Small Meeting Room\",\"id\":2}}"

Any help appreciated.

A: 

I tend to return dates as strings, so just do:

new Date(1286535600000).toString("MM/dd/yyyy") for example.

So, you may want to have your property with a getter that returns a string, so you can have it formatted, and perhaps the setter should also be a string, to simplify what is being passed back and forth from the page.

James Black
A: 

Thanks for the response James. In the end I used a different converter with Json.net and everything appears to work as planned. It does essentially return a formatted date string, but I can decorate the current DateTime property instead of using string in my .net class:-

    [JsonProperty("start")]
    [JsonConverter(typeof(IsoDateTimeConverter))]
    public DateTime Start
    {
        get { return _start; }
        set { _start = value; }
    }
donkeykong