0

我需要映射一个文件夹结构,该结构遵循 Dropbox 使用的类似模式。当用户向上导航树时,REST 服务器返回额外的 JSON 数据,这些数据需要使用 Restkit 映射到核心数据。我的 JSON 如下:

{
    "object_id": "19BEB55D78EA431EA555CA7ADD72DCA6",
    "class": "sfaccount",
    "files": [
        {
            "file_id": "78234782437892438792487942",
            "bytes": 0,
            "created": "2012-11-12T03:42:55.0000000",
            "modified": "2012-11-12T03:42:55.0000000",
            "path": "/",
            "is_dir": true,
            "contents": [
                {
                    "file_id": "980234890234890234980234890",
                    "name": "file1.xls",
                    "created": "2012-11-12T03: 42: 55.0000000",
                    "modified": "2012-11-12T03: 42: 55.0000000",
                    "path": "/file1.xls",
                    "is_dir": false,
                    "mime_type": "text/xls",
                    "bytes": 78810
                },
                {
                    "file_id": "924384238903429802439802890",
                    "name": "file2.pdf",
                    "created": "2012-11-12T03: 42: 55.0000000",
                    "modified": "2012-11-12T03: 42: 55.0000000",
                    "path": "/file2.pdf",
                    "is_dir": false,
                    "mime_type": "text/pdf",
                    "bytes": 15350
                },
                {
                    "file_id": "980349082498024390832490249",
                    "name": "sub folder 1",
                    "created": "2012-11-12T03: 42: 55.0000000",
                    "modified": "2012-11-12T03: 42: 55.0000000",
                    "path": "/sub folder 1",
                    "is_dir": true,
                    "bytes": 0
                }
            ]
        }
    ]
};

如果用户向上导航到“子文件夹 1”,则 JSON 将是:

{
    "object_id": "19BEB55D78EA431EA555CA7ADD72DCA6",
    "class": "sfaccount",
    "files": [
        {
           "file_id": "980349082498024390832490249",
           "name": "sub folder 1",
           "created": "2012-11-12T03: 42: 55.0000000",
           "modified": "2012-11-12T03: 42: 55.0000000",
           "path": "/sub folder 1",
           "is_dir": true,
           "bytes": 0,
            "contents": [
                {
                    "file_id": "564765785685856856658567575",
                    "name": "file1.xls",
                    "created": "2012-11-12T03: 42: 55.0000000",
                    "modified": "2012-11-12T03: 42: 55.0000000",
                    "path": "/sub folder 1/file1.xls",
                    "is_dir": false,
                    "mime_type": "text/xls",
                    "bytes": 78810
                },
                {
                    "file_id": "345687656675856790676786789",
                    "name": "file2.pdf",
                    "created": "2012-11-12T03: 42: 55.0000000",
                    "modified": "2012-11-12T03: 42: 55.0000000",
                    "path": "/sub folder 1/file2.pdf",
                    "is_dir": false,
                    "mime_type": "text/pdf",
                    "bytes": 15350
                },
                {
                    "file_id": "434546785689689667679988698",
                    "name": "sub folder 2",
                    "created": "2012-11-12T03: 42: 55.0000000",
                    "modified": "2012-11-12T03: 42: 55.0000000",
                    "path": "/sub folder 1/sub folder 2",
                    "is_dir": true,
                    "bytes": 0
                }
            ]
        }
    ]
}

我不确定模型结构以及如何使用 Restkit 创建映射。

4

1 回答 1

0

这是您将用来映射该 JSON 对象的对象映射。FileObject 类中的“contents”属性应该是一个 NSArray,并将填充一个 FileObject 数组。

RKObjectMapping *fileMapping = [RKObjectMapping mappingForClass:[FileObject class]];

[fileMapping addAttributeMappingsFromArray:@[@"file_id",@"bytes",@"created",@"modified",@"path",@"is_dir"]];
RKRelationshipMapping *subFileMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"contents" toKeyPath:@"contents" withMapping:fileMapping];

[fileMapping addPropertyMapping:subFileMapping];
于 2013-01-30T03:19:00.880 回答