1

I've found a million posts about serializing .NET dates to json. Yet, I'm unable to get any of those to work correctly.

Here is my serialzier code so far:

string javascriptJson = JsonConvert.SerializeObject(result, 
   new JavaScriptDateTimeConverter());
return Content(javascriptJson, "application/json");

This returns an unescaped string containing the correct JSON, and looks completely right when viewing it in the browser. (e.g. dates looks like : date: new Date(023198928) and as far as my limited javascript skill goes, that is how it should look like)

However, I can not get JQuery to understand this, either I get back a plain javascript string, or it simply fails, I want the deserialized object.

I've tried both $.get and $.getJSON

Ideas ?

2 Answers 2

1

These are not the mothods you are looking for...

jQuery.get():
Retrieve one of the DOM elements matched by the jQuery object.

jQuery.getJSON():
Load JSON-encoded data from the server using a GET HTTP request.

Try jQuery.parseJSON() instead:
Takes a well-formed JSON string and returns the resulting JavaScript object.

Sign up to request clarification or add additional context in comments.

2 Comments

Tried jQuery.parseJSON() but it throws and complains about unexpected token, however var res = eval(data); works correctly
Plain JavaScript eval() - Nice! It seems I didn't see the wood for the trees. Again... It's strange that parseJSON() fails, though. If you have the time, you should find out why - and post it here to satisfy my curiosity ;-)
0

It turns out that even though the response from my service is valid javascript, it is not valid Json.

You can not return "{ date : new Date(24342) }" as it opens up for script injection I guess. I reverted back to the standard .NET Json serializer and simply parsed the "\Date(343)\" that it returns..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.