Take the 2-minute tour ×

so I am happily slamming data into my system using JSON and setValuesForKeysWithDictionary until I run into an object that has an NSDate property.

The JSON contains dates with the following format:

BackOrderDate: "2011-03-15T00:00:00"

This gives me an error:

'NSInvalidArgumentException', reason: 'Unacceptable type of value for attribute: property = "BackOrderDate"; desired type = NSDate; given type = __NSCFString; value = 2011-03-15T00:00:00

I am using JSON.NET with MVC and the following settings with it:

JsonSerializerSettings serializerSettings = new JsonSerializerSettings();
serializerSettings.Converters.Add(new Newtonsoft.Json.Converters.IsoDateTimeConverter());
string json = JsonConvert.SerializeObject(objects, Formatting.None, serializerSettings);
return Content(json);

Any ideas on how to deal with this issue? I do not want to lose the use of setValuesForKeysWithDictionary since it so fricking fast.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

I assume you're using Core Data (since Core Data produces the error you posted) and that your entity has a property named “BackOrderDate” of type “Date”.

The problem is that JSON doesn't have a “Date” data type. In your .NET code you've handled this by telling the JSON serialize to convert dates to IS-formatted strings.

You need to perform the opposite conversion when you parse the JSON data in your iOS app, before you pass the data to setValuesForKeysWithDictionary:.

You can parse a string into an NSDate using an NSDateFormatter, and you can easily find lots of examples of that on the web.

How you perform the parsing before calling setValuesForKeysWithDictionary: depends on what you're using the parse the JSON.

If you're using Apple's NSJSONSerialization, you should probably use the NSJSONReadingMutableContainers option. Then go into the dictionary and replace the date string with an NSDate object before calling setValuesForKeysWithDictionary:.

If you're using some other JSON parser (like SBJSON), I don't know the best way to do the replacement.

share|improve this answer
Here is a good alternate solution as well: cimgf.com/2011/06/02/saving-json-to-core-data –  Slee Aug 8 '12 at 19:51

Your Answer


