使用Restkit映射文件夹结构

时间:2013-01-10 17:32:02

标签: ios json mapping restkit

我需要映射一个文件夹结构,该结构遵循Dropbox使用的类似模式。当用户导航树时,REST服务器返回需要使用Restkit映射到核心数据的其他JSON数据。我的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创建映射。

1 个答案:

答案 0 :(得分:0)

这是用于映射该JSON对象的对象映射。 FileObject类中的“contents”属性应该是NSArray,并使用FileObjects数组填充。

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