簡體   English   中英

JSON解析向iOS返回null(json字符串看起來正確)

[英]JSON parsing returns null to iOS (json string looks correct)

我正在嘗試將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字符串看起來正確:

我的字符串= [{“目標”:{“數量”:“ 100.0”,“ created_at”:“ 2011-08-20T00:55:34Z”,“ id”:1,“名稱”:“用戶”,“ updated_at “:” 2011-08-20T00:55:34Z“}},{”目標“:{”金額“:” 200.0“,” created_at“:” 2011-08-20T00:56:48Z“,” id“: 2,“名稱”:“ User2”,“ updated_at”:“ 2011-08-20T00:56:48Z”}},{“目標”:{“金額”:“ 19999.0”,“ created_at”:“ 2011-08 -20T19:15:10Z“,” id“:3,”名稱“:”這是我的目標“,” 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(從另一個類)的行:

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

目標名稱和目標金額為空。

這可能不是問題的一部分,但您應該調用[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方法要查找的屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM