I try to add NSDate
to NSMutableDictionary
, and then make it a JSon
string with this methods:
NSMutableDictionary *d = [[NSMutableDictionary alloc]init];
[d setObject:[NSDate date] forKey:@"date"];
NSData *jsondata = [[CJSONSerializer serializer] serializeDictionary:d error:nil];
NSString *requestBody = [[[NSString alloc] initWithData:jsondata encoding:NSUTF8StringEncoding] autorelease];
And i have couple of issue:
1) The jsondata
is nil, and then the requestBody
is nil too.
2) The [NSDate date]
getting me wrong date, not like the date in my iPhone:
2013-01-19 11:48:46 +0000
Any help with this issue?
Edit:
When i print the error i got this msg:
Error Domain=TODO_DOMAIN Code=-1 "Cannot serialize data of type '__NSDate'" UserInfo=0x177850 {NSLocalizedDescription=Cannot serialize data of type '__NSDate'}
It's not possible to do it?
It seems very unlikely that the dictionary would fail to insert the date. It seems much more likely that the problem is with the
CJSONSerializer
.Try the following:
You should see an error printed to the console.
UPDATE
OK, so it looks like we were right, and CJSONSerializer can't serialise dates. So you need to turn your date into a string. This is done with NSDateFormatter.
Hopefully you should now get the formatted date in your JSON. You can vary the format by changing the string you pass to
stringWithDate:
. If you're doing this a lot, you'll want a single date formatter as creating them is costly.