使用RestKit将NSArray映射到具有动态嵌套属性的JSON?

我如何使用RestKit将NSArray映射到具有动态嵌套属性的JSON中?

我有一个我将称之为' DataModel '的类,它与其他各种属性一起包含NSString*属性DataModel_tag 。 我还有一个UploadObj类,其唯一目的是为上传DataModel数组提供基础。 因此, UploadObj包含一个属性:

@property NSMutableArray* DataModels;

这需要按以下格式映射到JSON:

{
    "DataModels" : 
    {
        "DataModel tag 1" : 
            {
                // other properties of DataModel instance 1 here
            },
        "tag for DataModel 2" : 
            {
                // other properties of DataModel instance 2 here
            }
    }
}

我目前正在尝试遵循RestKit对象映射文档,并提出了以下方法:

+(RKObjectMapping*)mappingForDataModel {

    RKObjectMapping* mapping = [RKObjectMapping mappingForClass:
                                    [DataModel class]];

    [mapping mapKeyOfNestedDictionaryToAttribute:@"DataModel_tag"];

    mapping.forceCollectionMapping = YES;
    mapping.rootKeyPath = @"DataModel_tag";

    NSArray* propertyNames = // all properties, names obtained by introspection

    for (NSInteger i = 0; i < [propertyNames count]; i++) {
        NSString* propertyName = [propertyNames objectAtIndex:i];
        if (![propertyName isEqualToString:@"DataModel_tag"]) {
            // map (DataModel_tag).propertyName to propertyName
            [mapping mapKeyPath:[NSString stringWithFormat:
                @"(DataModel_tag).%@", propertyName]
                toAttribute:propertyName];
        }
    }

    return mapping;
}

+(void)configureMappingProviderForUpload {

    RKObjectMapping* uploadMapping = [Utilities
                                      simpleMappingForObject:[UploadObj class]
                                      withFieldsOrNil:nil];
    // ^ method not shown, but it just maps non-array properties to the object
    // with the same name. This method works in other parts of the code.
    [[RKObjectManager sharedManager] setSerializationMIMEType:RKMIMETypeJSON];
    [[[RKObjectManager sharedManager] router]
        routeClass:[UploadObj class]
        toResourcePath:@"/test.php"];

    RKObjectMapping *dataModelMapping = [Utilities mappingForDataModel];

    [uploadMapping mapKeyPath:@"DataModels"
        toRelationship:@"DataModels"
        withMapping:dataModelMapping];

    [[[RKObjectManager sharedManager] mappingProvider]
        setSerializationMapping:[dataModelMapping inverseMapping]
        forClass:[DataModel class]];

    [[[RKObjectManager sharedManager] mappingProvider]
        setSerializationMapping:[uploadMapping inverseMapping]
        forClass:[UploadObj class]];   
}

当我试图映射这个对象时,我收到每个DataModel属性的输出:

Destination object {
    "<RK_NESTING_ATTRIBUTE>" = [DataModel_tag value for current instance]
} rejected attribute value [attribute value for current attribute on 
current instance] for keyPath (DataModel_tag).[current attribute name]. 
Skipping...

我如何正确映射这个对象?

谢谢!

链接地址: http://www.djcxy.com/p/49195.html

上一篇: Using RestKit to map an NSArray into JSON with dynamic nested attributes?

下一篇: Can someone explain mapping Restkit Get results mapping into an Array