4

I need to parse JSON data from a server and make a list of object istances.

I'm using DataContract in order to make an association between json dictionary fields and class properties, however I've a problem: one of these fields contains a date in string from (something like "2011-01-01 15:00 UTC"); I wanna put this inside a DateTime property.

How can I convert this string to a datetime and pass it the property automatically using DataContract? is that possible?

danielemm
  • 1,636
  • 1
  • 14
  • 24
  • If you want to use Json.Net [Here](http://stackoverflow.com/questions/8639315/how-to-create-a-json-net-date-to-string-custom-converter) is an answer. – L.B Feb 29 '12 at 13:45
  • In fact I've choosed DataContract because it allows me to convert easily from JSON dictionaries to runtime classes. What's the difference with JSON.net? – danielemm Feb 29 '12 at 16:12
  • 1
    You have much more control on serialization/deserialization process. See the link for ex in my previous comment. You can also easily make use of `dynamic` since it can deserialize the object to a dictionary. Here are some working examples. http://pastebin.com/JEYfgL3a . Better take a look at [it's home page](http://json.codeplex.com/) – L.B Feb 29 '12 at 19:21

2 Answers2

8

You could use a property for the purpose:

[DataMember(Name="Foo")]
public string FormattedFoo {
    get { return /* apply some custom formatting to 'Foo' */; }
    set { Foo = /* apply some custom parsing to 'value' */; }
}
public DateTime Foo {get;set;}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • I've tried as you've suggested but I've this error: http://pastebin.com/jbtDej3C. Any idea marc? – danielemm Mar 01 '12 at 14:07
  • @malcom are you using the Parse? or the ParseExact? or...? and what is the sample input? – Marc Gravell Mar 01 '12 at 14:20
  • Both parse and parse exact return the same error. Value is a string in form "2012/01/01 00:00:00" – danielemm Mar 01 '12 at 14:24
  • @malcom swap the `-` for `/` in your format, then; testing here with `"yyyy/MM/dd HH:mm:ss"` works fine – Marc Gravell Mar 01 '12 at 14:39
  • Ops my mistake. The date is like this: "2012-02-21 12:41:23". Datecontract property code is this: http://pastebin.com/58MfNmmZ. The code crashes at http://imgur.com/JGHku (while parsing the data property. Data seems parsed okay but something happend when I try to assign it to my DateTime object) – danielemm Mar 05 '12 at 13:40
  • the first time I try to parse it works, a the second time it crashes :-/ that's so strange, it's drive me crazy – danielemm Mar 05 '12 at 13:44
  • okay I'm so sorry but the problem seems related to another property (wrong parsed). I was confused by the DateTime refer from debug log. Thank you so much, it's now ok :-) – danielemm Mar 05 '12 at 14:11
0

Put the DataMember attribute on the field instead of the property and use the setter/getter to make the conversion:

const string DATE_TIME_FORMAT = "<your format>";

[DataMember]
string myDate;

public DateTime MyDate {
  get 
  {
    return DateTime.ParseExact(myDate, DATE_TIME_FORMAT, CultureInfo.CurrentCulture);
  }
  set 
  {
    myDate = value.ToString(DATE_TIME_FORMAT);
  }
}
Maxence
  • 12,868
  • 5
  • 57
  • 69