I am trying to dynamically map JSON information to different objects. But I can't figure out how to get the whole pointer situation under control, because what I was hoping would work.
So far, my methodology has been to create a dictionary for each object by matching variable pointers with equivalent JSON keys. In my JSON mapping, I have a parseJSONFromDictionary:withObject , which should parseJSONFromDictionary:withObject over the dictionary returned by SBJSON JSONValue and assign the appropriate values โโto the appropriate variables in this object.
-(NSObject *)parseJSONFromDictionary:(NSDictionary *)dict withObject:(NSObject *)start{ for (NSString *key in dict) { if ([[self.mappings objectForKey:key] isMemberOfClass:[NSString class]]) { start.[self.mappings objectForKey:key] = [[dict valueForKey:key] stringValue]; } } return start; }
mappings is a dictionary that has variables and json keys, and dict is what is returned from the json parser.
The big problem is that I cannot start.[self.mappings objectForKey:key] . I donโt know much about C, and obviously I donโt know enough about pointers, so how can I start creating such a system? I know this is not the right way to do this, but what is the right way? I know that this can be done since RestKit does this, but they do not support OAuth, so I, unfortunately, cannot use their beautiful framework.
The reason I'm going this route is because the API I'm working with is currently in alpha stage. I want to be able to easily adapt to any future changes without rewriting many lines of code. I also want to start DRY-ly programming. I know that JSON parsing is very repetitive, and I would like to find a way to reduce overhead.
Thanks for any help!
EDIT: There seems to be some confusion as to what I ask. I do not need help in parsing JSON. I am already using SBJSON . I don't need help creating queries; I already use the JDG OAuthConsumer framework . I can only use frameworks that support OAuth 2.0.
I need help determining how to prevent :
-(Class1 *)parseJsonForClass1:(NSString *)inputString { NSDictionary *outputDict = [inputString JSONValue]; Class1 *instance1 = [self mapObjectsForClass1From:outputDict]; return instance1; } -(Class2 *)parseJsonForClass2:(NSString *)inputString { NSDictionary *outputDict = [inputString JSONValue]; Class2 *instance2 = [self mapObjectsForClass2From:outputDict]; return instance2; } -(Class3 *)parseJsonForClass3:(NSString *)inputString { NSDictionary *outputDict = [inputString JSONValue]; Class2 *instance3 = [self mapObjectsForClass3From:outputDict]; return instance3; } -(Class1 *)mapObjectsForClass1From:(NSDictionary *)dict { Class1 *object1 = [[Class1 alloc] init]; object1.name = [[dict valueForKey:@"name"] stringValue]; object1.date = [[dict valueForKey:@"date"] stringValue]; object1.objId = [[dict valueForKey:@"id"] intValue]; return object1; } -(Class2 *)mapObjectsForClass2From:(NSDictionary *)dict { Class2 *object2 = [[Class2 alloc] init]; object2.something = [[dict valueForKey:@"something"] stringValue]; object2.title = [[dict valueForKey:@"title"] stringValue]; object2.imageUrl = [[dict valueForKey:@"image_url"] stringValue]; return object2; } -(Class3 *)mapObjectsForClass3From:(NSDictionary *)dict { Class3 *object3 = [[Class3 alloc] init]; object3.foo = [[dict valueForKey:@"bar"] stringValue]; object3.flag = [[dict valueForKey:@"is_member"] boolValue]; object3.obj1 = [[Class1 alloc] init]; object3.obj1 = [self mapOjectsForClass1From:[dict objectForKey:@"name1"]]; return object3; }
So let me know if you have any suggestions for combining these methods into one method ...
json ios objective-c iphone dry
Mishiemoo
source share