我正在尝试解析 json 响应以用于核心数据模型。这是来自 json 的示例:
{
"@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 的实体的一对多关系,该实体将保留该级别的属性列表并且将具有多对关系带有实体 ProgramLogo(来自 json 文件中的“ProgramLogos”) Genres(将添加流派字符串数组作为仅包含一个字符串属性的多个 Genre 实体),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:@{
@"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数组(流派,不同实体中的每个字符串,以及ProgramLogos(实体中的每个dict)
2) RestKit 不解析 programId (key "@programId") 为什么?键中的“@”会停止解析吗?
我明白了
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFDictionary 0x958f980> valueForUndefinedKey:]: this class is not key value coding-compliant for the key programId.'