// // main.m // RESTSample // // Created by Rob Fahrni on 12/30/11. // Copyright (c) 2011 Robert R. Fahrni. All rights reserved. // #import #import "JSONKit.h" // ----------------------------------------------- // A simple JSON string representing an object with a firstName and lastName. static NSString* const kSampleJSON = @"{\"firstName\": \"Rob\", \"lastName\": \"Fahrni\"}"; // ----------------------------------------------- // Our Person Object definition @interface Person : NSObject { @private NSString* _firstName; NSString* _lastName; } @property (copy) NSString* firstName; @property (copy) NSString* lastName; - (id)initWithDictionary:(NSDictionary*)dict; - (void)dealloc; @end // ----------------------------------------------- // Our Person Object implementation @implementation Person @synthesize firstName = _firstName; @synthesize lastName = _lastName; - (id)initWithDictionary:(NSDictionary *)dict; { if ((self = [super init])) { _firstName = [dict valueForKey:@"firstName"]; _lastName = [dict valueForKey:@"lastName"]; } return self; } - (void)dealloc; { [_lastName release]; [_firstName release]; [super dealloc]; } - (NSString*)description { // Overriding description allows you to print something meaningful // to the debug output window. In this case we'll print // Rob Fahrni. return [NSString stringWithFormat:@"%@ %@\n", _firstName, _lastName]; } @end // ----------------------------------------------- // Out main entry point for the application int main(int argc, char *argv[]) { // Pretend like you've called a REST service here and it returns a string. // We'll just create a string from the sample json constant at the top // of this file. NSString* responseJSON = [NSString stringWithFormat:@"%@", kSampleJSON]; // 1) Create a dictionary, from the result string, // using JSONKit's NSString category; objectFromJSONString. NSDictionary* dict = [responseJSON objectFromJSONString]; // 2) Dump the dictionary to the debug console. NSLog(@"Dictionary => %@\n", dict); // 3) Now, let's create a Person object from the dictionary. Person* person = [[[Person alloc] initWithDictionary:dict] autorelease]; // 4) Dump the contents of the person object // to the debug console. NSLog(@"Person => %@\n", person); return 0; }