我想解析一個JSON響應以便與核心數據模型一起使用。這是從JSON樣本:RestKit創建多對多關係
{
"@context": "TVSchedule",
"ReturnCode": "0",
"ReturnMessage": "Successful request",
"Channel": [
{
"ChannelId": "http%…..0",
"Program": [
{
"@programId": "http……..",
"Title": "Divorce Court",
"ProgramLogo": "http://00_180x101.png",
"ProgramLogos": [
{
"@size": "small",
"#text": "http://.png"
},
{
"@size": "large",
"#text": "191.png"
}
],
"ProgramDetailsURL": "http:9",
"PublishedStartTime": "2013-07-01T19:00:00",
"PublishedEndTime": "2013-07-01T19:30:00",
"Duration": "00:00:30:00",
"RatingInfo": {
"@system": "MPAA",
"@code": "TV-PG",
"@age": "10",
"Title": "Not recommended for children under 10 years",
"Logo": "http://"
},
"ShortDescription": "She says she cannot trust him .",
"Year": "2013",
"Genres": [
"Series",
"Reality",
"Public Affairs",
"News",
"Episodic"
]
},
我需要得到這個在CoreData在那裏我有一個名爲頻道的實體將持有的channelID的一個一對多與實體命名程序,將保持關係該級別上的屬性列表,並將與實體ProgramLogo(來自Json文件中的「ProgramLogos」)具有多對多關係流派(流派字符串陣列將作爲多個體裁實體添加,只包含一個字符串屬性),RatingsInfo實體(與相應詞典中的一對一關係),RatingsInfo並未出現在所有程序中...
以下是我使用的RestKit代碼:
RKEntityMapping *channelMapping = [RKEntityMapping mappingForEntityForName:kCDChannelEntity inManagedObjectStore:managedObjectStore];
channelMapping.identificationAttributes = @[ kCDChannelId ];
[channelMapping addAttributeMappingsFromDictionary:@{
kJsonChannelId : kCDChannelId
}];
RKEntityMapping *programMapping = [RKEntityMapping mappingForEntityForName:kCDProgramEntity inManagedObjectStore:managedObjectStore];
[programMapping addAttributeMappingsFromDictionary:@{
@"@programId" : kCDProgramId ,
@"ProgramLogo" : kCDProgramLogo,
@"ProgramDetailsURL" : kCDProgramDetailsUrl,
@"Duration" : kCDProgramDuration,
@"PublishedStartTime" : kCDProgramStartTime,
@"PublishedEndTime" : kCDProgramEndTime,
@"Title" : kCDProgramTitle,
@"Year" : kCDProgramYear,
@"ShortDescription" : kCDProgramShortDescription
}];
[RKObjectMapping addDefaultDateFormatterForString:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'" inTimeZone:nil];
[channelMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"Program" toKeyPath:kCDChannelHasProgramsRel withMapping:programMapping]];// ??
RKEntityMapping *parentalRatingMapping = [RKEntityMapping mappingForEntityForName:kCDParentalRatingEntity inManagedObjectStore:managedObjectStore];
[parentalRatingMapping addAttributeMappingsFromDictionary:@{
@"[email protected]": kCDParentalRatingAge,
@"[email protected]" : kCDParentalRatingCode,
@"Program.RatingsInfo.Logo" : kCDParentalRatingLogo,
@"[email protected]" : kCDParentalRatingSystem,
@"Program.RatingsInfo.Title" : kCDParentalRatingTitle}];
[programMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"Program.RatingsInfo" toKeyPath:kCDProgramHasParentalRatingRel withMapping:parentalRatingMapping]];// ??
RKEntityMapping *genresMapping = [RKEntityMapping mappingForEntityForName:kCDGenreEntity inManagedObjectStore:managedObjectStore];
[genresMapping addAttributeMappingsFromDictionary:@{
@"Program.Genres": kCDGenreName
}];
[programMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"Program.Genres" toKeyPath:kCDProgramHasGenres withMapping:genresMapping]];
RKEntityMapping *logoMapping = [RKEntityMapping mappingForEntityForName:kCDProgramLogoEntity inManagedObjectStore:managedObjectStore];
[logoMapping addAttributeMappingsFromDictionary:@{
@"[email protected]" : kCDLogoSize,
@"Program.ProgramLogos.#text" : kCDLogoText
}];
好,現在有兩個問題: 1)如何解析兩個非KVO陣列(流派,在不同的實體的每個字符串,和ProgramLogos(在一個實體的每個字典)
2 )RestKit不解析programId(鍵「@programId」)爲什麼?密鑰中的「@」是否會停止解析?
我得到
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFDictionary 0x958f980> valueForUndefinedKey:]: this class is not key value coding-compliant for the key programId.'
這就是我的想法...我不確定keyPaths ...他們是如何爲嵌入式對象和關係創建的 – user426132
您可以舉個例子嗎? – user426132
沒有任何實體需要它自己的響應描述符嗎? – user426132