2

我正在使用 RestKit 发布我的专辑类和他的嵌套集合 o 歌曲(歌曲类)

我找不到正确的方法来序列化包含专辑歌曲的 NSArray:它将整个集合序列化为一种 NSDictionary 的集合。

我正在使用 v 0.10.0 并使用 NSObjects 而不是 CoreData

这是我的json结构:

{"album":
  {"id":12,
  "source":"apple",
  "collection_name":"The Platinum Collection",  
  "artist_name":"Queen",
  "collection_id":"28007467",
  "genre_id":5,
  "thumb":"mythumburl.jpg",
  "creation_user_id":1,
  "songs":[
    {"id":16,
    "track_name":"Bohemian Rhapsody",
    "track_time_millis":353933,
    "disc_number":1,
    "track_number":1,
    "track_time":"00:05:53"},
    ...
  ]
}

这是我的实际映射:

RKObjectMapping *songMapping = [RKObjectMapping mappingForClass:[Song class]];

[songMapping mapKeyPath:@"id" toAttribute:@"code"];
[songMapping mapKeyPath:@"source" toAttribute:@"source"];
[songMapping mapKeyPath:@"album_id" toAttribute:@"album_id"];
[songMapping mapKeyPath:@"track_name" toAttribute:@"track_name"];
[songMapping mapKeyPath:@"track_time_millis" toAttribute:@"track_time_millis"];
[songMapping mapKeyPath:@"track_time" toAttribute:@"track_time"];

[songMapping mapKeyPath:@"disc_number" toAttribute:@"disc_number"];
[songMapping mapKeyPath:@"track_number" toAttribute:@"track_number"];
[songMapping mapKeyPath:@"preview_url" toAttribute:@"preview_url"];


[objectManager.mappingProvider setObjectMapping:songMapping forKeyPath:@"song"];
[objectManager.mappingProvider setSerializationMapping:[songMapping inverseMapping] forClass:[Song class]];

RKObjectMapping *albumMapping = [RKObjectMapping mappingForClass:[Album class]];
...
[albumMapping hasMany:@"songs" withMapping:songMapping];


// Setup our object mappings
[objectManager.mappingProvider setObjectMapping:albumMapping forKeyPath:@"album"];

[objectManager.mappingProvider setSerializationMapping:[albumMapping inverseMapping] forClass:[Album class]];
4

0 回答 0