在 iPhone 上使用 Restkit 嵌套 json
我现在只是想习惯 iPhone 应用程序的 Restkit,但遇到了困难。我试图从嵌套的 json 文件中获取数据。
[
{"person": {
"name": "joe",
"id": "1234",
"numbers":
[
{"value": "555-12125"},
{"value": "222-12125"}
]
}
}]
我像这样设置了 rkobjectmanager
RKObjectManager* manager = [RKObjectManager objectManagerWithBaseURL:@"http://localhost:3000"];
[manager loadObjectsAtResourcePath:@"/J.json?user_id=36995582&planner=insta-grammar" objectClass:[Person class] delegate:self] ;
[manager registerClass:[Person class] forElementNamed:@"person"];
[manager registerClass:[Numbers class] forElementNamed:@"numbers"];
,然后 person 类和numbers 类如下,
#import "Person.h"
@implementation Person
@synthesize _name,_id,_number;
+ (NSDictionary*)elementToPropertyMappings {
return [NSDictionary dictionaryWithKeysAndObjects:
@"name",@"name",
@"id",@"id",nil];
}
+ (NSDictionary*)elementToRelationshipMappings {
return [NSDictionary dictionaryWithKeysAndObjects:
@"numbers", @"numbers",
nil];
}
@end
#import "Numbers.h"
@implementation Numbers
@synthesize _number;
+ (NSDictionary*)elementToPropertyMappings {
return [NSDictionary dictionaryWithKeysAndObjects:
@"value",@"value",nil];
}
+ (NSString*)primaryKeyProperty {
return @"value";
}
+ (NSDictionary*)relationshipToPrimaryKeyPropertyMappings {
return [NSDictionary dictionaryWithObject:@"value" forKey:@"value"];
}
@end
但每次在调用 didloadobjects 函数后 _number 保持为空,而 name 和 id 变量填充得很好。 对此有什么想法吗? 我在 git 上尝试了这个例子,但无法让它工作,所以任何帮助将不胜感激。 谢谢 克
just trying to get used to restkit for iphone apps here at the moment and ive hit a wall. Im trying to get data from a nested json file.
[
{"person": {
"name": "joe",
"id": "1234",
"numbers":
[
{"value": "555-12125"},
{"value": "222-12125"}
]
}
}]
i set up the rkobjectmanager like so
RKObjectManager* manager = [RKObjectManager objectManagerWithBaseURL:@"http://localhost:3000"];
[manager loadObjectsAtResourcePath:@"/J.json?user_id=36995582&planner=insta-grammar" objectClass:[Person class] delegate:self] ;
[manager registerClass:[Person class] forElementNamed:@"person"];
[manager registerClass:[Numbers class] forElementNamed:@"numbers"];
then the person class and numbers class as follows
#import "Person.h"
@implementation Person
@synthesize _name,_id,_number;
+ (NSDictionary*)elementToPropertyMappings {
return [NSDictionary dictionaryWithKeysAndObjects:
@"name",@"name",
@"id",@"id",nil];
}
+ (NSDictionary*)elementToRelationshipMappings {
return [NSDictionary dictionaryWithKeysAndObjects:
@"numbers", @"numbers",
nil];
}
@end
#import "Numbers.h"
@implementation Numbers
@synthesize _number;
+ (NSDictionary*)elementToPropertyMappings {
return [NSDictionary dictionaryWithKeysAndObjects:
@"value",@"value",nil];
}
+ (NSString*)primaryKeyProperty {
return @"value";
}
+ (NSDictionary*)relationshipToPrimaryKeyPropertyMappings {
return [NSDictionary dictionaryWithObject:@"value" forKey:@"value"];
}
@end
but every time the _number remains empty after the didloadobjects function is called while the name and id variables fill up fine.
any ideas on this?.
ive tried the example on git but couldnt get it to work so any help would be appreciated.
thanks
g
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你需要做的是在Person.h中声明一个
NSArray *_numbers
成员变量,使其成为一个类似@property (nonatomic, keep) NSArray *_numbers
的属性并合成它在 Person.m 中,如@synthesizenumbers = _numbers
。然后你可以摆脱 _number 是什么,因为你不会存储单个数字对象,你将存储一个数字对象数组。您的 Numbers 类应该只是 Number,并且您应该合成您的数字变量,如
@synthesize number = _number;
。因此,您的 Number.m 应该如下所示因为您的属性是数字,所以映射字典值是
@"number"
,而您的源 JSON 键应该是@"value"
。然后您可以加载对象,如下所示(请注意,我们使用
Number
类作为@"numbers"
元素):有关完整示例,请参阅 目录示例 int RestKit github 项目
What you need to do is to declare an
NSArray *_numbers
member variable in Person.h, make it a property like@property (nonatomic, retain) NSArray *_numbers
and synthesize it in Person.m like@synthesize numbers = _numbers
. Then you can get rid of whatever _number is, since you won't be storing a single number object, you will be storing an array of number objects.Your Numbers class should instead just be Number, and you should synthesize your number variable like
@synthesize number = _number;
. So your Number.m should look likeBecause your property is number, the mapping dictionary value is
@"number"
, while your source JSON key should be@"value"
.Then you can load the objects like (note we are using our
Number
class for the@"numbers"
element):For a full example see the Catalog Example int the RestKit github project
仅供关注此事的任何人使用。最后使用他们刚刚与restkit一起发布的新OM2使这一切正常工作。非常好用且易于使用的改变
Just for anyone following up on this. Finally got this all working using the new OM2 that they have just released with restkit. very nice and easy to use change