You can make the JSON response of your remote method call be converted into the class of your choice. This is very useful to manipulate instances of your custom classes instead of only use NSDictionaries
To make this possible, the class you want the JSON object to be converted to should respond to initWithJson: to be initialized using the JSON object. (see Simple example)
If the JSON object returned by the WebService is an array, the convertion from the JSON object to the given Class will be done on the objects in this array, and not on the array itself. This way, an array of objects representing e.g. a person can be converted into an NSArray of Person objects.
For more complex objects, you can call initWithJson in the initWithJson method itself, and also use arrayWithJson:itemsClass: method of the NSArray(JSON) category (JSONRPC_Extensions.h). See More complex example
As an example, to handle JSON objects representing a person with fields "firstname" and "lastname":
{ firstname: "John", lastname: "Doe" }
Then you can define an "Person" Objective-C class (to convert those JSON objects) like this:
@interface Person : NSObject { NSString* firstName; NSString* lastName; } @property(nonatomic, retain) NSString* firstName; @property(nonatomic, retain) NSString* lastName; -(id)initWithJson:(NSDictionary*)json;
@implementation Perso @synthesize firstName, lastName; -(id)initWithJson:(NSDictionary*)json { if (self = [super init]) { self.firstName = [json objectForKey:@"firstname"]; self.lastName = [json objectForKey:@"lastname"]; } return self; } -(void)dealloc { [firstName release]; [lastName release]; [super dealloc]; }
Then, if you have a JSON-RPC method that returns a JSON representation of a person as a result, you can call it this way:
[[service callMethodWithName:@"getAPerson" parameters:nil] setResultClass:[Person class]];
And when the delegate method will be called, a Person instance, constructed from the returned JSON, will be passed as the second parameter:
-(void)methodCall:(JSONRPCMethodCall*)mc didReturn:(Person*)person error:(NSError*)error { // handle the Person object here }
If the JSON object returned by the WebService is more complex, you can call initWithJson (on other classes) from your initWithJson implementation and even use the -[NSArray initWithJson:itemsClass:] or +[NSArray arrayWithJson:itemsClass:] methods of the NSArray category to build complex instances.
For example, image your WebService also return a JSON object representing a family, like this:
{ father: { firstname:"John", lastname:"Doe" }, mother: { firstname:"Jane", lastname:"Doe" }, children: [ { firstname:"John Jr", lastname:"Doe" }, { firstname:"Jane Jr", lastname:"Doe" }] }
In this example, the object representing a family is itself composed of objects representing persons. If you want to create a "Family" Objective-C class to represent such object, you can implement it this way:
@interface Family : NSObject { Person* father; Person* mother; NSArray* children; } @property(nonatomic, retain) Person* father; @property(nonatomic, retain) Person* mother; @property(nonatomic, retain) NSArray* children; -(id)initWithJson:(NSDictionary*)json; @end
@implementation Family @synthesize father, mother, children; -(id)initWithJson:(NSDictionary*)json { if (self = [super init]) { self.father = [[[Person alloc] initWithson:[json objectForKey:@"father"]] autorelease]; self.mother = [[[Person alloc] initWithson:[json objectForKey:@"mother"]] autorelease]; self.children = [[[NSArray alloc] initWithson:[json objectForKey:@"children"] itemsClass:[Person class]] autorelease]; } return self; } -(void)dealloc { [father release]; [mother release]; [children release]; [super dealloc]; } @end