在 iPhone 上使用 Restkit 嵌套 json

发布于 2024-11-10 18:03:42 字数 1697 浏览 2 评论 0原文

我现在只是想习惯 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戏剧牡丹亭 2024-11-17 18:03:42

你需要做的是在Person.h中声明一个NSArray *_numbers成员变量,使其成为一个类似@property (nonatomic, keep) NSArray *_numbers的属性并合成它在 Person.m 中,如 @synthesizenumbers = _numbers。然后你可以摆脱 _number 是什么,因为你不会存储单个数字对象,你将存储一个数字对象数组。

您的 Numbers 类应该只是 Number,并且您应该合成您的数字变量,如 @synthesize number = _number;。因此,您的 Number.m 应该如下所示

@implementation Number
@synthesize number = _number;

+ (NSDictionary *)elementToPropertyMappings {  
    return [NSDictionary dictionaryWithKeysAndObjects:  
            @"value", @"number", nil];  
}

因为您的属性是数字,所以映射字典值是 @"number",而您的源 JSON 键应该是 @"value"

然后您可以加载对象,如下所示(请注意,我们使用 Number 类作为 @"numbers" 元素):

RKObjectManager* manager = [RKObjectManager objectManagerWithBaseURL:@"http://localhost:3000"];
[manager registerClass:[Person class] forElementNamed:@"person"];
[manager registerClass:[Number class] forElementNamed:@"numbers"];

[manager loadObjectsAtResourcePath:@"/J.json?user_id=36995582&planner=insta-grammar" delegate:self];

有关完整示例,请参阅 目录示例 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 like

@implementation Number
@synthesize number = _number;

+ (NSDictionary *)elementToPropertyMappings {  
    return [NSDictionary dictionaryWithKeysAndObjects:  
            @"value", @"number", nil];  
}

Because 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):

RKObjectManager* manager = [RKObjectManager objectManagerWithBaseURL:@"http://localhost:3000"];
[manager registerClass:[Person class] forElementNamed:@"person"];
[manager registerClass:[Number class] forElementNamed:@"numbers"];

[manager loadObjectsAtResourcePath:@"/J.json?user_id=36995582&planner=insta-grammar" delegate:self];

For a full example see the Catalog Example int the RestKit github project

丑疤怪 2024-11-17 18:03:42

仅供关注此事的任何人使用。最后使用他们刚刚与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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文