6 Apr 2008 11:22 AM #1
Date fields in Json response
Date fields in Json response
I don't know if this was asked before, I did a search but cannot find a topic about this.
I am using WCF as a Json service, and returning objects as json responses. The objects contain date fields and they appear shown below in the response:
I am binding these data to a grid with jsonstore but I cannot succeed in showing the date field on the grid. The column shows empty.
What should I do, can someone guide me please?
6 Apr 2008 5:07 PM #2
6 Apr 2008 11:25 PM #3
- Join Date
- Mar 2007
- Notts/Redwood City
- Vote Rating
Can't you change the way the server generates the JSON to make it generate
PublicationStartDate: new Date(2008, 3, 7, 3, 0, 0)Search the forum: http://www.google.com/coop/cse?cx=01...%3Az7of1ufqccu
Read the docs too: http://extjs.com/deploy/dev/docs/
7 Apr 2008 2:07 AM #4
7 Apr 2008 4:09 AM #5
Sure it is possible to change it on the server but I am using automatic serialization, I mean my entities are automatically serialized to json and I don't want to temper with that. I thought it would be easier to handle it on the Ext side.
9 Apr 2008 11:38 AM #6
I need a way to intercept data loading process and parse datetime fields manually, removing the "Date" string from the response.
Can someone please guide me?
9 Apr 2008 2:36 PM #7
9 Apr 2008 2:42 PM #8
You should think twice before serializing .NET DateTime. In your code you have to take timezones into account on both the client and the server. A much better way is to serialize strings. Then you have complete control of the format and Ext can convert them into dates (almost) automatically. On the server you just use Convert.ToDateTime() and you're good to go.
9 Apr 2008 3:55 PM #9
Serializing .Net DateTime is an option, but if I go into that I have to manually serialize all my classes and all the return values of methods that include DateTime. As I said in my second post I am using JSon serialization with DataContracts, I don't write a single line of code to serialize my objects to Json.
I wrote a JsonStore extension that successfully receives Json responses from .Net WCF services and fills comboboxes, grids etc. Only problem seems to be with the DateTime type. Instead of sacrificing all the benefits of automatic serialization, I think extending an Ext type or smth else might be more appropriate for this and future projects.
Record.create is worth looking at.
9 Apr 2008 4:06 PM #10
Its not what I meant. You can keep all the nice serialization from WCF but DateTime values is NOT a good idea to serialize. Its a much better approach to mark your public DateTime properties as string (you can still have a private backing field as DateTime, if you need that in your server-side code).
// C# where DateProperty is a DateTime object DateProperty.ToString("yyyy-MM-dd hh:mm:ss");