2014-03-03 41 views
1

json api有一个帖子列表,每个帖子都有一个像这个帖子一样的用户列表。列表中的RestKit对象映射

后,用户每个核心数据都有一个实体。

RestKit如何做对象映射。

{ 
    posts:[ 
     { 
      "postid": 1, 
      "posttext": "post1", 
      "likeusers":[ 
       { 
        "uid": 1, 
        "username": "user1" 
       }, 
       { 
        "uid": 2, 
        "username": "user2" 
       } 
      ] 
     }, 
     { 
      "postid": 2, 
      "posttext": "post2", 
      "likeusers":[ 
       { 
        "uid": 1, 
        "username": "user1" 
       }, 
       { 
        "uid": 2, 
        "username": "user2" 
       } 
      ] 
     } 
    ] 
} 
+0

我读了一本书“RestKit for iOS”,然后阅读了RestKit文档,在短时间内获得了太多信息,并且不知道这个问题。下次再提问时会做出尝试。 – lbsweek

回答

1

这里是如何做到的映射:

// User mapping 
RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:@"User" 
                inManagedObjectStore:yourManagedObjectStore]; 
userMapping.identificationAttributes = @[@"userID"]; 
[userMapping addAttributeMappingsFromDictionary:@{ 
                @"uid": @"userID", 
                @"username": @"username", 
                }]; 

// Post mapping 
RKEntityMapping *postMapping = [RKEntityMapping mappingForEntityForName:@"Post" 
                inManagedObjectStore:yourManagedObjectStore]; 
postMapping.identificationAttributes = @[@"postID"]; 
[postMapping addAttributeMappingsFromDictionary:@{ 
                @"postid": @"postID", 
                @"posttext": @"postText", 
                }]; 

[postMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"likeusers" 
                      toKeyPath:@"likeUsers" withMapping:userMapping]]; 

希望它能帮助。