2014-02-18 147 views
0

任何人都可以請幫我在restkit 0.20中映射這個json對象嗎?Restkit中嵌套json的動態對象映射0.20

"feeds" : [ 
    { 
    "id": 32131354, 
    "caption": "Blah Blah Blah.", 
    "story" : "blah blah someone blah blah someone else", 
    "story_tags": { 
     "0": [ 
      { 
      "id": 21231654, 
      "name": "Someone", 
      "offset": 0, 
      "length": 25, 
      "type": "page" 
      } 
     ], 
     "33": [ 
      { 
      "id": 3213212313, 
      "name": "Someone else", 
      "offset": 33, 
      "length": 12, 
      "type": "page" 
      } 
     ] 
    }, 
} 
{ 
... 
} 
] 

我真的很感激任何幫助。這就是我現在在做什麼:

RKEntityMapping *feedsMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([FacebookFeed class]) inManagedObjectStore:managedObjectStore]; 
feedsMapping.identificationAttributes = @[@"id"]; 
[feedsMapping addAttributeMappingsFromDictionary:@{ 

                @"caption" : @"caption", 
                @"id" : @"id", 
                @"story" : @"story", 
               }]; 

,這是關係映射

RKEntityMapping *facebookFeedStoryTagsMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([FacebookFeedStoryTag class]) inManagedObjectStore:managedObjectStore]; 
facebookFeedStoryTagsMapping.identificationAttributes = @[@"id"]; 
[facebookFeedStoryTagsMapping addAttributeMappingToKeyOfRepresentationFromAttribute:@"offset"]; 
[facebookFeedStoryTagsMapping addAttributeMappingsFromDictionary:@{ 
                   @"(offset).id" : @"id", 
                   @"(offset).length" : @"length", 
                   @"(offset).name" : @"name", 
                   @"(offset).type" : @"type", 
                   }]; 

[feedsMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"story_tags" toKeyPath:@"story_tags" withMapping:facebookFeedStoryTagsMapping]]; 

,但我得到以下錯誤:

restkit.object_mapping:RKMappingOperation.m:745 Key path 'story_tags' yielded collection containing another collection rather than a collection of objects: (
    (
      { 
     id = 31314654654; 
     length = 25; 
     name = "Someone"; 
     offset = 0; 
     type = page; 
    } 
) 
) 

回答

0

的JSON "0": [介紹,因爲一個問題我們真的想在那裏有一個字典(這就是你的映射的寫法),但我們實際上有一個數組。這是你看到的錯誤的來源和含義。

理想既然這樣你FacebookFeedStoryTag需要整個陣列(和它包含的字典)爲變形屬性映射你會改變JSON ...

。這是因爲映射無法處理嵌套數組。

您可以將此可變形屬性設置爲瞬態並實現自定義設置器/ willSave方法來獲取數組並提取字典內容。由於鍵名匹配,可以使用setValuesForKeysWithDictionary:簡單完成(儘管您需要處理offset鍵)。

+0

我正在修改服務器端的JSON來解決這個問題,但我想知道是否有方法可以直接在對象映射中進行。我想在服務器端避免額外的循環來改變JSON。 –