2011-08-21 4 views
0

Je suis en train d'obtenir un JSON à l'application iOS, mais continue à obtenir des valeurs NULL ..JSON retours parsing null à iOS (chaîne JSON semble correct)

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

chaîne JSON semble correcte:

my string = [{"goal": {"amount": "100.0", "created_at": "2011-08-20T00: 55: 34Z", "id": 1, "nom": "Utilisateur", " updated_at ":" 2011-08-20T00: 55: 34Z "}}, {" goal ": {" amount ":" 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," nom ":" Ceci est MON BUT "," updated_at ":" 2011-08-20T19: 15: 10Z "}}, {" goal ": {" amount ":" 0.0 "," created_at ":" 2011-08-20T20: 46: 44Z "," id ": 4, "name": "but", "updated_at": "2011-08-20T20: 46: 44Z"}}]

je manque quelque chose que je suppose de base ..

MISE À JOUR:

Voici une ligne qui renvoie NULL (d'une autre classe):

- (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.name et goal.amount sont nuls ..

+0

Que retourne-t-il en tant que NULL? –

Répondre

1

Ce ne peut pas faire partie de votre question, mais vous devriez être appeler [self init] (ou plus important encore, [super init] par héritage):

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

aussi:

for (NSDictionary *dictionary in results) { 
     Goal *goal = [[Goal alloc] initWithDictionary: 
      [dictionary objectForKey:@"goal"]]; 
     [goals addObject:goal]; 
     [goal release]; 
    } 

changement clé est [dictionary objectForKey:@"goal"] dans la ligne 3.

Le tableau JSON est d'objets avec un seul goal membre, avec les propriétés que votre méthode initWithDictionary recherche.

+0

Cela a totalement fonctionné! Merci beaucoup!! et btw .. quand j'ajoute la ligne if (self = [self init]), j'obtiens cet avertissement: Problème sémantique: Utiliser le résultat d'une affectation comme une condition sans parenthèses – Stpn

+0

Cet avertissement est juste parce que le compilateur s'assure que vous fait l'affectation ('=') dans la condition if à dessein (puisque vous voulez généralement '==', pour la comparaison d'égalité). L'ajout d'un second ensemble de parenthèses (à savoir: '((self = [self init]))') supprime l'avertissement, car cette utilisation de '=' est prévue. –