2013-07-01 116 views
0

我想解析一個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.' 

回答

0

它看起來像你的channelResponseDescriptor應該有@ 「通道」 的的keyPath。很難說所有常數是否還有其他問題。看來只是最高級別的描述符需要更多的信息應該在哪裏應用。


通常,您創建的所有映射都是相互關聯的。根據你的JSON的結構,很有可能你不需要這麼多的響應描述符,只是通道的頂級響應描述符,然後所有的映射都用來使用關係在結構中向下導航。


例如,你有JSON:

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

你可以有渠道和程序實體和映射之間,這樣當信道映射將導航到對應關係之間的關係JSON的程序部分,映射程序然後連接對象。您似乎是這樣的:

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

(其代碼中的錯字,它應該被應用到channelMapping,而不是programMapping

所以programResponseDescriptor可能是不需要的,可能只是原因您的映射結果將包含重複的項目。

+0

這就是我的想法...我不確定keyPaths ...他們是如何爲嵌入式對象和關係創建的 – user426132

+0

您可以舉個例子嗎? – user426132

+0

沒有任何實體需要它自己的響應描述符嗎? – user426132