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 6887431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:53:03+00:00 2026-05-27T05:53:03+00:00

I defined some custom classes, such as Teacher , Student … Now I receive

  • 0

I defined some custom classes, such as Teacher, Student…
Now I receive teacher info (JSON string) from remote server.

How can I convert the JSON string to Teacher object.

In Java, it’s easy to implement a common method for all class (Teacher, Student…) with reflect.

But in Objective-C on iOS, the best way I can find is to use Entity of Core Data, which has setValue:forKey method. First I convert the JSON string to NSDictionary, the set the key-value pair in the disctionary to the Entry.

Is there any better ways?

(I’m from China, so maybe my English is poor, sorry!)

  • 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-27T05:53:03+00:00Added an answer on May 27, 2026 at 5:53 am

    These are all good frameworks for JSON parsing to dictionaries or other primitives, but if you’re looking to avoid doing a lot of repetitive work, check out http://restkit.org . Specifically, check out https://github.com/RestKit/RestKit/blob/master/Docs/Object%20Mapping.md This is the example on Object mapping where you define mapping for your Teacher class and the json is automagically converted to a Teacher object by using KVC. If you use RestKit’s network calls, the process is all transparent and simple, but I already had my network calls in place and what I needed was to convert my json response text to a User object (Teacher in your case) and I finally figured out how. If that’s what you need, post a comment and I’ll share how to do it with RestKit.

    Note: I will assume the json is output using the mapped convention {"teacher": { "id" : 45, "name" : "Teacher McTeacher"}}. If it’s not this way, but instead like this {"id" : 45, "name" : "Teacher McTeacher"} then don’t worry … object mapping design doc in the link shows you how to do this…a few extra steps, but not too bad.

    This is my callback from ASIHTTPRequest

    - (void)requestFinished:(ASIHTTPRequest *)request {
        id<RKParser> parser = [[RKParserRegistry sharedRegistry] parserForMIMEType:[request.responseHeaders valueForKey:@"Content-Type"]]; // i'm assuming your response Content-Type is application/json
        NSError *error;
        NSDictionary *parsedData = [parser objectFromString:apiResponse error:&error];
        if (parsedData == nil) {
            NSLog(@"ERROR parsing api response with RestKit...%@", error);
            return;
        }
    
        [RKObjectMapping addDefaultDateFormatterForString:@"yyyy-MM-dd'T'HH:mm:ssZ" inTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; // This is handy in case you return dates with different formats that aren't understood by the date parser
    
        RKObjectMappingProvider *provider = [RKObjectMappingProvider new];
    
        // This is the error mapping provider that RestKit understands natively (I copied this verbatim from the RestKit internals ... so just go with it
        // This also shows how to map without blocks
        RKObjectMapping* errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];
        [errorMapping mapKeyPath:@"" toAttribute:@"errorMessage"];
        [provider setMapping:errorMapping forKeyPath:@"error"];
        [provider setMapping:errorMapping forKeyPath:@"errors"];
    
        // This shows you how to map with blocks
        RKObjectMapping *teacherMapping = [RKObjectMapping mappingForClass:[Teacher class] block:^(RKObjectMapping *mapping) {
            [mapping mapKeyPath:@"id" toAttribute:@"objectId"];
            [mapping mapKeyPath:@"name" toAttribute:@"name"];
        }];
    
        [provider setMapping:teacherMapping forKeyPath:@"teacher"];
    
        RKObjectMapper *mapper = [RKObjectMapper mapperWithObject:parsedData mappingProvider:provider];
        Teacher *teacher = nil;
        RKObjectMappingResult *mappingResult = [mapper performMapping];
        teacher = [mappingResult asObject];
    
        NSLog(@"Teacher is %@ with id %lld and name %@", teacher, teacher.objectId, teacher.name);
    }
    

    You can obviously refactor this to make it cleaner, but that now solves all my problems.. no more parsing… just response -> magic -> Object

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

Sidebar

Related Questions

I have some custom error classes defined, each with their own functionality (not shown):
I've some classes defined in a dll file. These are in the form of
On my blog I use some CSS classes which are defined in my stylesheet,
I've successuflly created a custom prefpane for my application (in thunderbird) and defined some
I've written some custom configuration collections, elements etc. Now, I'd like to do a
I have been having some trouble lately with using custom classes as types. As
Consider one has some user defined types, and containers of those types that are
I have some logic, which defines and uses some user-defined types, like these: class
For some reason this program is saying that 'switch' is not defined. What is
some times i see that functions are defined as below: read_dir(dir) char *dir; {

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.