使用RestKit序列化嵌套集合的正确方法

时间:2012-08-10 09:25:05

标签: ios restkit

我正在使用RestKit用他的嵌套集合歌曲(歌曲课程)来发布我的专辑课程

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

我正在使用v 0.10.0并使用NSObject而不是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]];

0 个答案:

没有答案