Note that there are some explanatory texts on larger screens.

plurals
  1. POObjective-C DRY JSON Mapping and Object Creation
    text
    copied!<p>I am trying to dynamically map JSON information into different objects. But I can't figure out quite how to get the whole pointer situation under control, as what I was hoping would work does not.</p> <p>So far my methodology has been to create a dictionary for each object, mapping the variable pointers to the equivalent JSON keys. In my JSON mapper, I have a method, <code>parseJSONFromDictionary:withObject</code> that is supposed to iterate over the dictionary returned by <a href="http://code.google.com/p/json-framework/" rel="noreferrer">SBJSON</a>'s JSONValue and assign the appropriate values to the appropriate variables in the given object.</p> <pre><code>-(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; }</code></pre> <p><code>mappings</code> is the dictionary that has the variables and json keys, and the <code>dict</code> is what gets returned from the json parser.</p> <p>The big problem is that I can't do <code>start.[self.mappings objectForKey:key]</code>. I don't know much about C, and clearly I don't know enough about pointers, so how would I go about creating this kind of system? I know that's not the right way to do this, but what is the right way? I know it can be done, as <a href="http://restkit.org" rel="noreferrer">RestKit</a> does it, but they don't support OAuth so I sadly can't use their lovely framework.</p> <p>The reason I'm going this route is because the API I'm working with is currently in its alpha stage. I want to be able to easily adapt to any future changes without having to rewrite many lines of code. I also want to start programming DRY-ly. I know JSON parsing is very repetitive, and I'd like to find a way to reduce the amount of overhead.</p> <p>Thanks for any and all help!</p> <hr> <p><strong>EDIT:</strong> It seems there is some confusion as to what I'm asking. I <em>do not</em> need help parsing JSON. I'm using <a href="http://code.google.com/p/json-framework/" rel="noreferrer">SBJSON</a> already. I <em>do not</em> need help making requests, I am already using <a href="https://github.com/jdg/oauthconsumer" rel="noreferrer">JDG's OAuthConsumer framework</a>. I <em>can only</em> use frameworks that support OAuth 2.0.</p> <p><em>I do need help</em> figuring out how to <strong>prevent</strong> this:</p> <pre><code>-(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; }</code></pre> <p>So please let me know if you have any suggestions on how to combine these kinds of methods into 1 method...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload