我正在尝试获取iOS应用程序的JSON,但继续获取NULL值..
- (id)initWithDictionary:(NSDictionary *)dictionary {
self.name = [dictionary valueForKey:@"name"];
self.amount = [NSString stringWithFormat:@"%@",
[dictionary valueForKey:@"amount"]];
self.goalId = [dictionary valueForKey:@"id"];
self.createdAt = [dictionary valueForKey:@"created_at"];
self.updatedAt = [dictionary valueForKey:@"updated_at"];
return self;
}
+ (NSArray *)findAllRemote {
NSURL *url = [NSURL URLWithString:@"http://localhost:3000/goals.json"];
NSError *error = nil;
NSString *jsonString =
[NSString stringWithContentsOfURL:url
encoding:NSUTF8StringEncoding
error:&error];
NSLog(@"my string = %@", jsonString);
NSMutableArray *goals = [NSMutableArray array];
if (jsonString) {
SBJSON *json = [[SBJSON alloc] init];
NSArray *results = [json objectWithString:jsonString error:&error];
[json release];
for (NSDictionary *dictionary in results) {
Goal *goal = [[Goal alloc] initWithDictionary:dictionary];
[goals addObject:goal];
[goal release];
}
}
return goals;
}
- (id)initWithDictionary:(NSDictionary *)dictionary {
self.name = [dictionary valueForKey:@"name"];
self.amount = [NSString stringWithFormat:@"%@",
[dictionary valueForKey:@"amount"]];
self.goalId = [dictionary valueForKey:@"id"];
self.createdAt = [dictionary valueForKey:@"created_at"];
self.updatedAt = [dictionary valueForKey:@"updated_at"];
return self;
}
+ (NSArray *)findAllRemote {
NSURL *url = [NSURL URLWithString:@"http://localhost:3000/goals.json"];
NSError *error = nil;
NSString *jsonString =
[NSString stringWithContentsOfURL:url
encoding:NSUTF8StringEncoding
error:&error];
NSLog(@"my string = %@", jsonString);
NSMutableArray *goals = [NSMutableArray array];
if (jsonString) {
SBJSON *json = [[SBJSON alloc] init];
NSArray *results = [json objectWithString:jsonString error:&error];
[json release];
for (NSDictionary *dictionary in results) {
Goal *goal = [[Goal alloc] initWithDictionary:dictionary];
[goals addObject:goal];
[goal release];
}
}
return goals;
}
JSON字符串看起来正确:
my string = [{“goal”:{“amount”:“100.0”,“created_at”:“2011-08-20T00:55:34Z”,“id”:1,“name”:“User” “的updated_at”: “2011-08-20T00:55:34Z”}},{ “目标”:{ “量”: “200.0”, “created_at”: “2011-08-20T00:56:48Z”,” ID “:2”,名称 “:” 用户2" , “的updated_at”: “2011-08-20T00:56:48Z”}},{ “目标”:{ “量”: “19999.0”, “created_at”:” 2011-08-20T19:15:10Z“,”id“:3,”name“:”这是我的目标“,”updated_at“:”2011-08-20T19:15:10Z“}},{”目标“ :{ “量”: “0.0”, “created_at”: “2011-08-20T20:46:44Z”, “ID”:4 “名称”: “目标”, “的updated_at”:“2011-08-20T20 :46:44Z“}}]
我想遗失一些基本的东西......
更新:
这是一行返回NULL(来自另一个类):
goal.name 和 goal.amount 是空的..
答案 0 :(得分:1)
这可能不属于您的问题,但您应该通过继承来调用[self init]
(或更重要的是,[super init]
):
- (id)initWithDictionary:(NSDictionary *)dictionary {
if (self = [self init]) {
self.name = [dictionary valueForKey:@"name"];
self.amount = [NSString stringWithFormat:@"%@",
[dictionary valueForKey:@"amount"]];
self.goalId = [dictionary valueForKey:@"id"];
self.createdAt = [dictionary valueForKey:@"created_at"];
self.updatedAt = [dictionary valueForKey:@"updated_at"];
}
return self;
}
此外:
for (NSDictionary *dictionary in results) {
Goal *goal = [[Goal alloc] initWithDictionary:
[dictionary objectForKey:@"goal"]];
[goals addObject:goal];
[goal release];
}
第3行的主要变化为[dictionary objectForKey:@"goal"]
。
JSON数组是具有单个goal
成员的对象,具有initWithDictionary
方法正在查找的属性。