2011-08-21 70 views
0

我想獲得一個JSON到iOS應用程序,但不斷收到NULL值..JSON解析返回NULL的iOS(JSON字符串看起來是正確的)

- (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」, 「標識」: 4, 「名」: 「目標」, 「的updated_at」: 「2011-08-20T20:46:44Z」}}]

我失去了一些東西基本我想..

UPDATE

這裏是返回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是空..

+0

什麼是作爲NULL返回? –

回答

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方法正在查找的屬性。

+0

這完全工作!非常感謝!!和btw ..當我添加if(self = [self init])行時,我得到這個警告:語義問題:使用賦值結果作爲沒有括號的條件 – Stpn

+0

這個警告只是因爲編譯器確保你'有意識地在if條件中執行賦值('=')(因爲您通常需要'=='來進行相等比較)。添加第二組括號(例如:'((self = [self init]))')將會抑制警告,因爲這是'='的用法。 –