RestKit RKObjectMapping 嵌套 json
Posted
技术标签:
【中文标题】RestKit RKObjectMapping 嵌套 json【英文标题】:RestKit RKObjectMapping nested json 【发布时间】:2012-04-16 00:43:53 【问题描述】:如何使用 RestKit RKObjectMapping 映射下面的嵌套 json?我只需要映射“数据”。我下面的当前代码返回 (null) - 谢谢!
"sucess": true,
"error":
"code": "",
"message": ""
,
"data": [
"id": 1,
"name": "Salad",
"description": "Salad Desc",
"category_id": 1
,
"id": 2,
"name": "Soup",
"description": "Soup Desc",
"category_id": 1
,
"id": 3,
"name": "Wrap",
"description": "Wrap Desc",
"category_id": 1
,
"id": 4,
"name": "Appetizers",
"description": "Appetizers Desc",
"category_id": 1
]
类
@interface Subcategory : NSObject
@property (nonatomic, retain) NSNumber *subcategoryID;
@property (nonatomic, retain) NSNumber *categoryID;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *description;
@property (nonatomic, retain) NSString *thumbnail;
@property (nonatomic, retain) NSArray *data;
视图控制器
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[Subcategory class]];
RKObjectMapping* dataMapping = [RKObjectMapping mappingForClass:[Subcategory class]];
[dataMapping mapKeyPath:@"id" toAttribute:@"subcategoryID"];
[dataMapping mapKeyPath:@"category_id" toAttribute:@"categoryID"];
[dataMapping mapAttributes:@"name", @"description", nil];
[mapping mapKeyPath:@"data" toRelationship:@"data" withMapping:dataMapping];
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:[NSString stringWith
【问题讨论】:
为了他人的利益,github.com/RestKit/RestKit/wiki/Object-mapping 是 RK 对象映射的一个很好的入门资源。 快速提问,我不是 ObjC 专家,但有人告诉我 NSString 属性应该是“复制”而不是“保留”,但我看到很多开源项目中保留了 NSString 属性并在此处发帖 - @steamboy 在这种情况下使用“保留”是否有原因? 【参考方案1】:您缺少 rootKeyPath 并且还在提供程序中设置映射:
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[Subcategory class]];
RKObjectMapping* dataMapping = [RKObjectMapping mappingForClass:[Subcategory class]];
[dataMapping mapKeyPath:@"id" toAttribute:@"subcategoryID"];
[dataMapping mapKeyPath:@"category_id" toAttribute:@"categoryID"];
[dataMapping mapAttributes:@"name", @"description", nil];
dataMapping.rootKeyPath = @"data"; //this is what you were missing
//get your mapping provider (i'm not sure if you already have on declared)
[mappingProvider setObjectMapping:dataMapping forResourcePathPattern:@"/somepath/toservice/"];
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/somepath/toservice/"];
这里有更多信息来回答您关于获取其他元素的问题(例如“错误”):
- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error
NSError *parseError = nil;
id response = [objectLoader.response parsedBody:&parseError];
DLog(@"response %@", response);
if (!parseError)
NSString *errorMessage = [[response valueForKey:@"error"] valueForKey:@"message"];
//do something with it
【讨论】:
我添加了 dataMapping = @"data";我收到此错误“从 NSString 分配给 'RKObjectMapping *_strong' 的指针类型不兼容” 很抱歉我的错字。你需要 dataMapping.rootKeyPath 你测试了吗?我在自己的代码中做了类似的事情,所以我知道 rootKeyPath 有效。 成功了!谢谢!顺便说一句,我将如何映射“成功”和“错误”?我需要添加它们以将它们添加到标题中,就像“数据”一样 老实说,我不确定那个...我相信您确实会在 restkit 代表调用中得到完整的响应,因此您可以手动查看。以上是关于RestKit RKObjectMapping 嵌套 json的主要内容,如果未能解决你的问题,请参考以下文章
RestKit RKObjectMapping Swift 可选
RestKit RKObjectMapping 嵌套 json
CocoaPods 更新 RestKit #import "RKObjectMapping.h" 文件未找到
如何使用带有 RKObjectMapping 的 RestKit 0.24 将本地 JSON 字符串映射到对象?
使用 RKParams 向 API 发布帖子并使用 RestKit 使用 RKObjectMapping 映射响应
RestKit:给定 RKObjectMapping、object.class 和 JSON 字符串,如何获取映射对象?