iPhone JSON 解析

发布于 2024-11-07 11:27:50 字数 539 浏览 0 评论 0原文

我是 iPhone 应用程序开发新手。 我需要一些帮助...我想解析一些数据(银行名称)并将它们显示到 UITableView 中。我正在使用此代码来解析 JSON 格式的银行名称:

     NSArray *atmDAta = [responseString JSONValue];
for (NSDictionary *dict in atmDAta) {
    NSLog(@"%@", [dict objectForKey:@"Name"]);
    [listOfItems addObject:[dict objectForKey:@"Name"]];
}

但是当我尝试在表中显示它们时,如下所示:

 cell.textLabel.text = [listOfItems objectAtIndex:indexPath.row];
return cell;

这不会在表中显示任何内容,但 NSlog 正在工作并且显示 xcode 日志中的银行名称。请帮忙。

I am new in iPhone app Development.
I need some help... I want to parse some data (Banks Name) and show them into a UITableView. I'm using this code to Parse Banks Name which is in JSON Format :

     NSArray *atmDAta = [responseString JSONValue];
for (NSDictionary *dict in atmDAta) {
    NSLog(@"%@", [dict objectForKey:@"Name"]);
    [listOfItems addObject:[dict objectForKey:@"Name"]];
}

But when i try to show them in the the Table like this :

 cell.textLabel.text = [listOfItems objectAtIndex:indexPath.row];
return cell;

This will show me nothing in the table but NSlog is working and show me the banks name in the xcode log. Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

始终不够爱げ你 2024-11-14 11:27:50

完成 json 解析后调用 [yourtable reloadData];

Call [yourtable reloadData]; when you finished json parsing

烟雨凡馨 2024-11-14 11:27:50

请参考此代码进行 JSON 解析

-(void)getLatestScore
 {
     SBJsonParser *parser = [[SBJsonParser alloc] init];    

     NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://json-cricket.appspot.com/score.json"]];

     // Perform request and get JSON back as a NSData object
     NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

     // Get JSON as a NSString from NSData response
     NSString *str_Json_String = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

     // parse the JSON response into an object

     NSDictionary *dicCricketScore = [parser objectWithString:str_Json_String error:nil];

     lblDate.text = [dicCricketScore objectForKey:@"date"];
     lblMatch.text = [dicCricketScore objectForKey:@"match"];
     lblBattingTeam.text = [dicCricketScore objectForKey:@"batting_team"];
     lblScore.text = [dicCricketScore objectForKey:@"score"];
     lblSummary.text = [dicCricketScore objectForKey:@"summary"];
 }

从此代码中,您将从 JSON 解析中获得最新的板球得分......

Please refer this code for JSON Parsing

-(void)getLatestScore
 {
     SBJsonParser *parser = [[SBJsonParser alloc] init];    

     NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://json-cricket.appspot.com/score.json"]];

     // Perform request and get JSON back as a NSData object
     NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

     // Get JSON as a NSString from NSData response
     NSString *str_Json_String = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

     // parse the JSON response into an object

     NSDictionary *dicCricketScore = [parser objectWithString:str_Json_String error:nil];

     lblDate.text = [dicCricketScore objectForKey:@"date"];
     lblMatch.text = [dicCricketScore objectForKey:@"match"];
     lblBattingTeam.text = [dicCricketScore objectForKey:@"batting_team"];
     lblScore.text = [dicCricketScore objectForKey:@"score"];
     lblSummary.text = [dicCricketScore objectForKey:@"summary"];
 }

From this code you will get Latest cricket score from JSON Parsing....

习惯成性 2024-11-14 11:27:50

@Rocky 尝试一下

在 .h 中声明
NSArray *listOfItems;

@property (nonatomic, copy)NSArray *listOfItems;

并在 .m 中使用它

NSMutableArray *tempArray = [[NSmutableArray alloc]initWithCapacity:[atmDAta count]];
for (NSDictionary *dict in atmDAta) {
    NSLog(@"%@", [dict objectForKey:@"Name"]);
    if (![dict valueForKeyIsNull:@"Name"]) {
        [tempArray addObject:[dict objectForKey:@"Name"]];          
    }
}
self.listOfItems = [NSArray arrayWithArray:tempArray];
[tempArray release];
if([self.listOfItems count] > 0)
[yourTable reloadData];

,我正在使用类别

@implementation NSDictionary (KeyExists)

- (BOOL) keyExists:(NSString *) key {
    return [self valueForKey:key] != nil;
}

- (BOOL) valueForKeyIsNull:(NSString *) key {
    return ![self keyExists:key] || ((NSNull *)[self valueForKey:key] == [NSNull null]);
}
@end

干杯!

@Rocky Give this a try

Declare in .h
NSArray *listOfItems;

@property (nonatomic, copy)NSArray *listOfItems;

and use this in .m

NSMutableArray *tempArray = [[NSmutableArray alloc]initWithCapacity:[atmDAta count]];
for (NSDictionary *dict in atmDAta) {
    NSLog(@"%@", [dict objectForKey:@"Name"]);
    if (![dict valueForKeyIsNull:@"Name"]) {
        [tempArray addObject:[dict objectForKey:@"Name"]];          
    }
}
self.listOfItems = [NSArray arrayWithArray:tempArray];
[tempArray release];
if([self.listOfItems count] > 0)
[yourTable reloadData];

Where I am using a Category

@implementation NSDictionary (KeyExists)

- (BOOL) keyExists:(NSString *) key {
    return [self valueForKey:key] != nil;
}

- (BOOL) valueForKeyIsNull:(NSString *) key {
    return ![self keyExists:key] || ((NSNull *)[self valueForKey:key] == [NSNull null]);
}
@end

Cheers!!!

嘿嘿嘿 2024-11-14 11:27:50

添加数组后,您可以执行 [listOfItemsretain]

After adding array you can do [listOfItems retain]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文