0

我正在尝试解析 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.'
4

1 回答 1

0

看起来您channelResponseDescriptor的 keyPath 应该是 @"Channel"。由于所有常量,很难判断是否还有其他问题。似乎只有顶级描述符需要更多关于它应该应用在哪里的信息。


通常,您创建的所有映射都相互关联。根据您的 JSON 的结构,您很有可能不需要这么多响应描述符,只需要通道的顶级描述符,然后所有映射都用于使用关系向下导航结构。


例如,您有 JSON:

  "Channel": [
    {
      "ChannelId": "http%…..0",
      "Program": [
        {...

您可以在频道和节目实体之间建立关系,并在映射之间建立对应关系,这样当频道被映射时,它将导航到 JSON 的节目部分,映射节目,然后连接对象。你似乎有这个:

[channelMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:kJsonProgram toKeyPath:kCDChannelHasProgramsRel withMapping:programMapping]];

(您的代码中有错字,因为它应该应用于channelMapping,而不是programMapping

所以这programResponseDescriptor可能不是必需的,可能只会导致您的映射结果包含重复的项目。

于 2013-07-01T20:28:38.733 回答