1

我在我的应用程序中编写了代码,例如:

-(void)viewDidLoad
{

    SBJsonParser *parser = [[SBJsonParser alloc] init];

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@""]];

    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

    NSArray *statuses = [parser objectWithString:json_string error:nil];

    for (NSDictionary *status in statuses) {

        NSLog(@"%@ - %@", [status objectForKey:@"Name"], [status objectForKey:@"LoginUrl"] );
    }
}

这是我的 JSON。我只想在我的 UITableView 中获取名称、图像、登录网址。

JSON

0

Name : "Windows Live™ ID"

LoginUrl : ""

LogoutUrl : ""

ImageUrl : ""

EmailAddressSuffixes

1

Name : "Google"

LoginUrl : ""

LogoutUrl : ""

ImageUrl : ""

EmailAddressSuffixes

2

Name : "Yahoo!"

LoginUrl : ""

LogoutUrl : ""

ImageUrl : ""

EmailAddressSuffixes

我应该在我的 .m 文件中写什么?

4

2 回答 2

0

这是一个关于解析 JSON 并在表视图中显示的简单教程http://www.altinkonline.nl/tutorials/json/xcode-and-parsing-json/

注意:解析发生在- (void)viewWillAppear:(BOOL)animated- 除非您确实需要在每次视图出现时下载和解析数据,否则不要这样做

于 2013-04-03T09:55:06.450 回答
0

不幸的是,与 OSX 不同,您在 iOS 和 Cocoa-touch 中没有绑定

我假设您正在寻找的是标准的 UITableView 填充机制,这是通过实现 UITableView 的 Datasource 和 Delegate 方法来实现的。

这已经在很多线程中讨论过,但我会提醒你基本策略。

从您的 JSON 响应中,您有一个数组,每个数组都包含填充 UITableCellView 所需的数据。在cellForRowAtIndexPath: 你应该检查 indexPath 行(假设你只有一个部分),获取相应数组的元素,并使用它来设置 CellView 的属性。它看起来像这样:

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault     reuseIdentifier:CellIdentifier] autorelease];
    }
 NSDictionary *myJsonResponseIndividualElement = myJsonResponseElementsArray[indexPath.row];
 cell.textLabel.text = myJsonResponseIndividualElement[@"Name"];    
return cell;

}

于 2013-04-03T10:04:57.487 回答