0

JSON を iOS アプリに取得しようとしていますが、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;    
}

JSON 文字列は正しいようです:

私の文字列 = [{"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,"name":"User2","updated_at":"2011-08-20T00:56:48Z"}},{"goal":{"amount":"19999.0","created_at":"2011-08 -20T19:15:10Z","id":3,"name":"これが私の目標です","updated_at":"2011-08-20T19:15:10Z"}},{"目標":{" amount":"0.0","created_at":"2011-08-20T20:46:44Z","id":4,"名前":"目標","updated_at":"2011-08-20T20:46:44Z"}}]

私が推測する基本的な何かが欠けています..

更新

(別のクラスから) NULL を返す行を次に示します。

- (IBAction)refresh {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
self.goals = [Goal findAllRemote];
[self.tableView reloadData];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

- (void)viewDidLoad
{
[super viewDidLoad];

self.title = @"Goals";
self.navigationItem.leftBarButtonItem = self.editButtonItem;

UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] 
                                      initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
                                  target:self 
                                  action:@selector(refresh)];
self.navigationItem.rightBarButtonItem = refreshButton;
[refreshButton release];

[self refresh];

}


 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


static NSString *CellIdentifier = @"GoalCellId";

UITableViewCell *cell = 
[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                                   reuseIdentifier:CellIdentifier] autorelease];
}

Goal *goal = [goals objectAtIndex:indexPath.row];

cell.textLabel.text = goal.name;
cell.detailTextLabel.text = goal.amount;

return cell;
}

goal.namegoal.amountが Null です。

4

1 に答える 1

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];
    }

キーの変更は[dictionary objectForKey:@"goal"]3 行目です。

JSON 配列は、メソッドが探してgoalいるプロパティを持つ単一のメンバーを持つオブジェクトの配列です。initWithDictionary

于 2011-08-21T19:50:41.600 に答える