在表视图中列出jSON文件的元素

时间:2014-02-26 18:00:23

标签: ios objective-c json uitableview

我想在Table View

中列出此jSON文件中的所有元素
{
   "GetBuildingsRadiusJsonResult":[
      {
         "COD_SIG":"5001402005001",
         "DISTANCIA":21.32146784390920,
         "IDTIPO":"2",
         "LATITUDE":38.73580937270080,
         "LONGITUDE":-9.15660678323934,
         "MORADA":"Rua Ramalho Ortigão, 20-20B"
      },
      {
         "COD_SIG":"5001402008001",
         "DISTANCIA":34.53373314061230,
         "IDTIPO":"2",
         "LATITUDE":38.73569047355290,
         "LONGITUDE":-9.15607623644036,
         "MORADA":"Rua Ramalho Ortigão, 14"
      },
      {
         "COD_SIG":"5000201021001",
         "DISTANCIA":29.13207744582850,
         "IDTIPO":"2",
         "LATITUDE":38.73539981163320,
         "LONGITUDE":-9.15636893767009,
         "MORADA":"Rua Ramalho Ortigão, 31-31A"
      },
      {
         "COD_SIG":"5000201023001",
         "DISTANCIA":34.608510460067,
         "IDTIPO":"2",
         "LATITUDE":38.73552941823310,
         "LONGITUDE":-9.15683701485913,
         "MORADA":"Rua Ramalho Ortigão, 39-39E"
      },
      {
         "COD_SIG":"5000201027001",
         "DISTANCIA":24.92522103436050,
         "IDTIPO":"2",
         "LATITUDE":38.73542668844180,
         "LONGITUDE":-9.15649968147357,
         "MORADA":"Rua Ramalho Ortigão, 33-33B"
      },
      {
         "COD_SIG":"5000201029001",
         "DISTANCIA":28.14768277223310,
         "IDTIPO":"2",
         "LATITUDE":38.735470995147,
         "LONGITUDE":-9.15669924136460,
         "MORADA":"Rua Ramalho Ortigão, 37-37B"
      },
      {
         "COD_SIG":"5000201028001",
         "DISTANCIA":25.19034723079050,
         "IDTIPO":"2",
         "LATITUDE":38.73544762568370,
         "LONGITUDE":-9.15660101384062,
         "MORADA":"Rua Ramalho Ortigão, 35-35B"
      },
      {
         "COD_SIG":"5000201033001",
         "DISTANCIA":36.53199372687960,
         "IDTIPO":"2",
         "LATITUDE":38.73537439211060,
         "LONGITUDE":-9.15624045741059,
         "MORADA":"Rua Ramalho Ortigão, 29-29B"
      },
      {
         "COD_SIG":"5001402004001",
         "DISTANCIA":13.64427058972170,
         "IDTIPO":"2",
         "LATITUDE":38.73576229086240,
         "LONGITUDE":-9.15640626273806,
         "MORADA":"Rua Ramalho Ortigão, 18-18B"
      },
      {
         "COD_SIG":"5001402012001",
         "DISTANCIA":23.90756953606770,
         "IDTIPO":"2",
         "LATITUDE":38.73573141844790,
         "LONGITUDE":-9.15621547369463,
         "MORADA":"Rua Ramalho Ortigão, 16-16A"
      }
   ]
}

我希望每行的名称都是“COD_SIG”值。

这是我的.h文件

#import <UIKit/UIKit.h>

@interface lxvProcessesViewController : UIViewController {
    IBOutlet UITableView *processTableView;

    NSArray *processes;
    NSMutableData *data;
}

@end

和.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    processTableView.delegate = self;
    processTableView.dataSource = self;
    // Do any additional setup after loading the view.

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    NSString* str = [NSString stringWithFormat:@"http://lxi.cm-lisboa.pt/wsgeospatialops2/geospatialopsrest.svc/GetBuildingsRadiusjson?longitude=-9.15647&latitude=38.73565&nResults=10"];
    NSURL *urlTenBuildings = [NSURL URLWithString:str];

    NSLog(@"URL --------  %@  ---------",str);

    NSURLRequest *request = [NSURLRequest requestWithURL:urlTenBuildings];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];

    NSLog(@"Entrou no viewDidLoad");

}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"Entrou no connection didReceiveResponse");
    data = [[NSMutableData alloc] init];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
    NSLog(@"Entrou no connection didReceiveData");
    [data appendData:theData];

    NSString* txt = [NSString stringWithUTF8String:[data bytes]];
    NSLog(@"Resultado do URL  ------ %@ -------", txt);
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"Entrou no connectionDidFinishLoading");

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    processes = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    [processTableView reloadData];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"Entrou no connection didFailWithError");
    UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Erro" message:@"O acesso não foi completado. Verifique que está ligado à Internet via 3G ou Wi-Fi." delegate:nil cancelButtonTitle:@"Cancelar" otherButtonTitles:nil];
    [errorView show];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

}

-(long) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(long)tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"%lu", (unsigned long)[processes count]);

    return 10;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Entrou no tableView");
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Main Cell"];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Main Cell"];
    }

    cell.textLabel.text = [[processes objectAtIndex:indexPath.row] objectForKey:@"COD_SIG"];

    NSLog(@"%@",cell.textLabel.text);

    return  cell;
}

我在这一行收到错误:

cell.textLabel.text = [[processes objectAtIndex:indexPath.row] objectForKey:@"COD_SIG"];

以下是控制台错误:NSInvalidArgumentException',原因:' - [__ NSCFDictionary objectAtIndex:]:无法识别的选择器发送到实例

非常感谢你的帮助!

4 个答案:

答案 0 :(得分:2)

JSON中的根元素是字典,而不是数组。

替换

processes = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

processes = [[NSJSONSerialization JSONObjectWithData:data options:0 error:nil] objectForKey:@"GetBuildingsRadiusJsonResult"];

从JSON获取数组。

修改numberOfRowsInSection返回

return [processes count];

答案 1 :(得分:1)

我认为由于numberOfRowsInSection

中的错误返回number_of_rows而收到错误
-(long)tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"%lu", (unsigned long)[processes count]);

    return 10;
}

应该是

-(long)tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"%lu", (unsigned long)[processes count]);

    return [processes count];
}

也替换,

processes = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

processes = [[NSJSONSerialization JSONObjectWithData:data options:0 error:nil] objectForKey:@"GetBuildingsRadiusJsonResult"];

因为你有NSDictionary的数据,而不是NSArray。

答案 2 :(得分:1)

这里的问题是你的字典包含数组,然后该数组再次包含字典。所以首先你需要解析字典,然后你需要解析数组。 试试这样: -

   cell.textLabel.text = [[[processes objectForKey:GetBuildingsRadiusJsonResult] objectAtIndex:indexPath.row] objectForKey:@"COD_SIG"];

答案 3 :(得分:0)

试试这个:

cell.textLabel.text = processes[@"GetBuildingsRadiusJsonResult"][indexPath.row][@"COD_SIG"]

忘记提及进程应该是字典而不是数组

相关问题