Ios RestKit创建多对多关系
我试图解析一个json响应,以便与核心数据模型一起使用。这是一个来自json的示例:Ios RestKit创建多对多关系,ios,objective-c,restkit,Ios,Objective C,Restkit,我试图解析一个json响应,以便与核心数据模型一起使用。这是一个来自json的示例: { "@context": "TVSchedule", "ReturnCode": "0", "ReturnMessage": "Successful request", "Channel": [ { "ChannelId": "http%…..0", "Program": [ {
{
"@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中获得这一点,我有一个名为Channel的实体,它将保存channelId,与名为program的实体有一对多的关系,该实体将保存该级别的属性列表,并与entities ProgramLogo(来自json文件中的“ProgramLogo”)类型有多对多的关系(体裁字符串数组将作为仅包含一个字符串属性的多个体裁实体添加),分级信息实体(对应字典中的一对一关系),分级信息不会出现在所有程序上
以下是我使用的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:@{
@"Program.RatingsInfo.@age": kCDParentalRatingAge,
@"Program.RatingsInfo.@code" : kCDParentalRatingCode,
@"Program.RatingsInfo.Logo" : kCDParentalRatingLogo,
@"Program.RatingsInfo.@system" : 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:@{
@"Program.ProgramLogos.@size" : kCDLogoSize,
@"Program.ProgramLogos.#text" : kCDLogoText
}];
好的,现在有两个问题:
1) 如何解析两个非KVO数组(类型、不同实体中的每个字符串和ProgramLogo(实体中的每个dict))
2) RestKit不解析programId(键“@programId”)为什么?键中的“@”是否停止解析
我明白了
***由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[valueForUndefinedKey:]:此类不符合密钥程序ID的键值编码。”
您的channelResponseDescriptor
的键路径应该是@“Channel”。由于所有的常量,很难判断是否还有其他错误。似乎只有顶层描述符需要更多关于应该在哪里应用它的信息
通常,您创建的所有映射都是相互关联的。根据JSON的结构,很可能您不需要太多响应描述符,只需要通道的顶级描述符,然后使用所有映射通过关系向下导航结构
例如,您有JSON:
"Channel": [
{
"ChannelId": "http%…..0",
"Program": [
{...
您可以在通道和程序实体之间建立关系,并在映射之间建立对应关系,以便在映射通道时,它将导航到JSON的程序部分,映射程序,然后连接对象。您似乎有以下情况:
[channelMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:kJsonProgram toKeyPath:kCDChannelHasProgramsRel withMapping:programMapping]];
(代码中有一个输入错误,因为它应该应用于频道映射
,而不是程序映射
)
因此,
programResponseDescriptor
可能不是必需的,它只会导致映射结果包含重复项。我也这么认为。。。我不确定关键路径是什么。。。它们是如何为嵌入式对象和关系创建的请举个例子好吗?任何实体都不需要自己的响应描述符吗?通道至少需要一个响应描述符。我删除了其他响应描述符,现在通道得到了处理,但没有其他任何东西。。。
[channelMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:kJsonProgram toKeyPath:kCDChannelHasProgramsRel withMapping:programMapping]];