Harsh Rajput Harsh Rajput - 6 months ago 28
JSON Question

Get only last vale of dictionary

I am new in iOS development and my problem is when i parse data than only last value is showing above value are not showing .Please solve my issue.
Array name city info get 3 value but when i want to display them it gives onle last value of array.

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:WebData1 options:0 error:0];
NSArray *city_info = [json objectForKey:@"city_info"];
for(NSDictionary *dic in city_info) {
NSMutableArray *vlcc_service_name = [dic objectForKey:@"vlcc_service_name"];

Centers = [[NSMutableArray alloc]initWithObjects:vlcc_service_name, nil];
}
// NSLog(@"%@", vlcc_service_name);
NSLog(@"%@", Centers);
[_CenterTableView reloadData];
}

Answer

Please try with this.

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:WebData1 options:0 error:0];
    NSArray *city_info = [json objectForKey:@"city_info"];
    NSMutableArray  *Centers = [[NSMutableArray alloc]init];
  for(NSDictionary *dic in city_info) {
    NSString *vlcc_service_name = [dic objectForKey:@"vlcc_service_name"];
   /NSLog(@"%@", vlcc_service_name);
    [Centers addObject: vlcc_service_name];
  }

NSLog(@"%@", Centers);
[_CenterTableView reloadData];
}