2

RestKit を使用して、入れ子になったコレクション o の曲 (Song クラス) を含む Album クラスを POST しています。

アルバムの曲を含む NSArray をシリアル化する正しい方法が見つかりません。コレクション全体を NSDictionary の一種のコレクションにシリアル化します。

v 0.10.0 を使用しており、CoreData ではなく NSObjects を使用しています

これが私の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