Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3499354
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:36:05+00:00 2026-05-18T12:36:05+00:00

I am trying to parse a json string straight into a managed object. The

  • 0

I am trying to parse a json string straight into a managed object.
The json string contains all strings, but my Entity has Date objects.

if this is my json string

{
"name":"John",
"dob": "12/12/2008",
etc...
}

and here’s my entity Person:

name : NSString
dob: Date
etc...

I want to parse that json straight through by looping through the keys of the Json dict, and setting the values to the matching keys of my entity:

Person *aPerson = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:managedObjectContext];
NSArray *keys = [jsonDict allKeys];

for (NSString *key in keys) {

 [aPerson setValue:[jsonDict valueForKey:key] forKey:key];

}

This only works if all my entity’s properties are NSStrings.

How can I get the type/class of my entity’s property to be able to set the various types?

eg.
if key = @”dob”
how can I get the type/class of aPerson.dob?

so my code will look something like:

Person *aPerson = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:managedObjectContext];
NSArray *keys = [jsonDict allKeys];

for (NSString *key in keys) {
    if([[aPerson objectForKey:key] isKindOfClass:[NSDate class]])
    {
       // Create an NSDate object
    }else{
       [aPerson setValue:[jsonDict valueForKey:key] forKey:key];
    }
}

thanks

UPDATE:

Ok I tried using the isKindOfClass and here’s the result was:

// for key = @"name" 
// aPerson.name is NSString
[[aPerson valueForKey:key] isKindOfClass:[NSString class]] => TRUE
[[aPerson valueForKey:key] isKindOfClass:[NSObject class]] => TRUE

// for key = @"age"
// aPerson.age is NSNumber
[[aPerson valueForKey:key] isKindOfClass:[NSNumber class]] => TRUE
[[aPerson valueForKey:key] isKindOfClass:[NSObject class]] => TRUE

// for key = @"dob"
// aPerson.dob is NSDate
[[aPerson valueForKey:key] isKindOfClass:[NSDate class]] => FALSE
[[aPerson valueForKey:key] isKindOfClass:[NSObject class]] => FALSE

how is that?
@dynamic dob and NSDate, is notOfClass NSDate!

please tell me what I am missing!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T12:36:06+00:00Added an answer on May 18, 2026 at 12:36 pm

    Thanks a lot to Rog, for sending me in the right direction, I ended up overriding my setter as below:

    -(void)setDob:(id)value
    {
        [self willAccessValueForKey:@"dob"];
        if ([value isKindOfClass:[NSString class]]) {
            [self setPrimitiveValue:[DateFormatter dateFromString:value] forKey:@"dob"];
        }else{
            [self setPrimitiveValue:value forKey:@"dob"];
        }
        [self didChangeValueForKey:@"dob"];
    }
    

    and all works great now, instead of a 1000 lines of code parsing, it’s all done in 3!
    nice one!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to parse an array of JSON objects into an array of strings
I'm trying to parse a JSON string into an array reference: my $str =
I'm trying to parse a simple JSON string, but I'm not used to do
I'm trying to parse a simple string in C++. I know the string contains
I'm trying to work with json-framework on iPhone to parse a json string. When
I'm trying to parse a very simple json object with 1 value. (ocUnit test
I am trying to parse to JSON object in java. I am posting json
I am trying to parse JSON in an Adobe Flex app, using http://www.mikechambers.com/blog/2006/03/28/tutorial-using-json-with-flex-2-and-actionscript-3/'>This Tutorial
I'm trying to parse some JSON using the JSon.Net library. The documentation seems a
I'm trying to parse an international datetime string similar to: 24-okt-08 21:09:06 CEST So

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.