iOS7有一个轻量级的JSON到对象映射器,(不是RestKit或NSJSONSerialization)



我目前的项目使用AFNetworking 2.2,当我添加Restkit时,通常拒绝编译。是否有一种方法让我得到一些等效的RKObjectMapping定义如下从其他轻量级库?我说的是将JSON转换为自定义值对象,而不仅仅是字典或数组。

我想到了Android版的Google GSON, iOS版有类似的东西吗?

我想完成什么:

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;
}

您尝试过NSJsonSerialization (https://developer.apple.com/library/ios/documentation/foundation/reference/nsjsonserialization_class/Reference/Reference.html)吗?它似乎满足了你的需要。我用它来解析JSON格式的东西,

-(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;
}

最新更新