RestKit将JSON对象节点粉碎成Core Data

时间:2012-02-22 17:42:05

标签: objective-c ios json restkit

我有一个JSON结构需要被分解成几个核心数据表。然而,我回来的JSON并没有真正标准化为一个结构,我可以弄清楚如何将它们分成对象。我有4张桌子,歌曲,艺术家,专辑,流派。

JSON:

{
"Songs": [
{
"strFileID": "b7eccc27-b099-4ea2-93c5-5004f6d2e31d",
"strFileName": "07-Cryin.mp3",
"strDuration": "00:05:09.812",
"strAlbumTitle": "Big Ones",
"strArtists": "Aerosmith",
"strTrackNumber": 7,
"strGenre": "Rock",
"strTitle": "07-Cryin.mp3"
}
]
}

由于strArtists没有被分割成它自己的对象并且只是一个元素,我无法弄清楚如何创建映射以确保只有“Aerosmith”被扯到它自己的表中然后再回传。

目前我有以下作为获得歌曲和艺术家的起点:

    RKManagedObjectMapping *artistMapping = [RKManagedObjectMapping mappingForClass:[Artist class]];
    artistMapping.primaryKeyAttribute = ArtistAttributes.artistName;
    [artistMapping mapKeyPath:@"Song.strArtists" toAttribute:ArtistAttributes.artistName];

    RKManagedObjectMapping *songMapping = [RKManagedObjectMapping mappingForClass:[Song class]];
    songMapping.primaryKeyAttribute = SongAttributes.fileId;
    [songMapping mapKeyPath:@"strFileID" toAttribute:SongAttributes.fileId];
    [songMapping mapKeyPath:@"strFileName" toAttribute:SongAttributes.fileName];
    [songMapping mapKeyPath:@"strTitle" toAttribute:SongAttributes.title];
    [songMapping mapKeyPath:@"strDuration" toAttribute:SongAttributes.duration];
    [songMapping mapKeyPath:@"strTrackNumber" toAttribute:SongAttributes.trackNumber];

    [songMapping mapRelationship:@"artist" withMapping:artistMapping];

    [objectManager.mappingProvider setMapping:songMapping forKeyPath:@"Song"];
    [objectManager.mappingProvider setMapping:artistMapping forKeyPath:@"strArtists"];

0 个答案:

没有答案