Objective-C Creation and Creation of DRY JSON

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 ...

+6
json ios objective-c iphone dry
source share
4 answers

I recently noticed that someone is doing this with Objective Resources . While this is a good way to access objects, some of the implementations can become messy. And you need to use everything that the server sends you, whether you like it or not. I'm going to stay away from this right now.

+2
source share

I know this is an old thread, but thought it was here, the code in https://github.com/mystcolor/JTObjectMapping could help with object mapping. You will need to have mappings and your custom object before using this. I found this on googling .... havent used it though. Must try.

+6
source share

You can use KVC. Create a setter for each value in the JSON dictionary that you want to insert into your object, and use -setValue:forKey: to actually set the values, for example.

 -(NSObject *)parseJSONFromDictionary:(NSDictionary *)dict withObject:(NSObject *)start { for (NSString *key in [dict allKeys]) { NSString* propertyName = [[self mappings] objectForKey: key]; [start setValue: [dict objectForKey: key] forKey: propertyName]; // the above line will throw an exception if start does not have a set accessor for property } return start; } 
+5
source share

Also see HTTPRiot , which takes care of most communication efforts from your shoulder, handles connections asynchronously, etc. This iOS JSON Relationship Tutorial (Ruby bent but minimal) shows how to use it; part of the code is deprecated, but generally receives the message through.

0
source share

All Articles