-1

My current project uses AFNetworking 2.2 and in general refuses to compile when I add Restkit. Is there a way for me to get some equivalent of RKObjectMapping as defined below from some other lightweight library? I'm talking about taking JSON and turning it into a custom value object, not just a dictionary or array.

Google GSON for Android comes to mind, is there something like that for iOS?

What' I'm trying to accomplish:

static RKObjectMapping* mapping = nil;

+(RKObjectMapping*)objectMapping
{

    if(mapping != nil)
    {
        return mapping;
    }

    //allows automatic unpacking of JSON payloads into  Value Objects
    //https://github.com/RestKit/RestKit/wiki/Object-Mapping


    //JSON - VO
    mapping = [RKObjectMapping mappingForClass:[MyVO class]];
    [mapping addAttributeMappingsFromDictionary:@{
                                                         @"label": @"label",
                                                         @"icon": @"iconName",
                                                         @"action": @"actionName",
                                                         @"children": @"children"
                                                         }];

    return mapping;

}
Alex Stone
  • 46,408
  • 55
  • 231
  • 407
  • Use NSJSONSerialization to produce an NSDictionary, then write an `initWithDict:` method for your class. Gives you complete control, and not really any more difficult than doing the "automatic" object mapping for anything beyond the most trivial case. – Hot Licks Aug 18 '14 at 15:37
  • This might be useful http://stackoverflow.com/questions/25213707/parsing-json-to-a-predefined-class-in-objective-c/25213946#25213946 – modusCell Aug 18 '14 at 20:55
  • This is what I'm currently using, but was hoping there's something to make this simpler. – Alex Stone Aug 18 '14 at 20:56
  • 1
    I have struggled a lot about this this is easiest way. a call matches with your json then just call it. if you want to extend the class at runtime you can add properties with matching new keys in your json http://stackoverflow.com/questions/7819092/how-can-i-add-properties-to-an-object-at-runtime – modusCell Aug 18 '14 at 21:00

2 Answers2

1

Have you tried NSJsonSerialization (https://developer.apple.com/library/ios/documentation/foundation/reference/nsjsonserialization_class/Reference/Reference.html)? It seems to accomplish what you need. I use it whenever I need to parse anything in JSON format,

Jessica
  • 249
  • 5
  • 12
0
-(id) initWithDictionary:(NSDictionary*) dict {
    id self = [super init];
    if (id) {
        self.label = dict[@"label"];
        self.icon = dict[@"iconName"];
        self.action = dict[@"actionName"];
        self.children = dict[@"children"];
    }
    return id;
}
Hot Licks
  • 47,103
  • 17
  • 93
  • 151