RestKit 将 JSON 对象节点分解为核心数据

发布于 2025-01-08 02:42:43 字数 1607 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文