RestKit 将 JSON 对象节点分解为核心数据
我有一个 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"];
I have a JSON structure that needs to be shredded into several core data tables. However with the JSON I am getting back isn't really normalized to a structure I can figure out how to split them into objects. I have 4 tables, Songs, Artist, Album, Genre.
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"
}
]
}
Since strArtists isn't split into it's own object and is just an element, I can't figure out how to create the mapping to ensure only "Aerosmith" is ripped out into it's own table and then related back.
Currently I have the following as a starting point to just get song and the artist:
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"];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论