select器视图iPhone的JSON数组

我从JSON Web服务获取这些数据

List ARRAY: ( { assets = ( { identity = 34DL3611; systemId = 544507; }, { identity = 34GF0512; systemId = 5290211; }, { identity = 34HH1734; systemId = 111463609; }, { identity = 34HH1736; systemId = 111463622; }, { identity = 34YCJ15; systemId = 294151155; } ); identity = DEMO; systemId = 4921244; }) 

通过使用这个代码:

 NSArray *list =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; NSLog(@"Response data: %@", responseString); NSLog(@"List ARRAY: %@", list); NSDictionary *dict = [list objectAtIndex: 0]; NSMutableArray *vehicleGroups = [dict objectForKey:@"identity"]; NSLog(@"Vehicle Groups: %@", vehicleGroups); 

这是我正在使用的select器代码:

  -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 1;} - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent :(NSInteger)component { return [vehicleGroups count];} -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{return nil;} -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ } 

该应用程序崩溃在线

 return [vehicleGroups count]; 

pickerView委托方法numberOfRowsInComponent 。 我不明白 – 为什么我面临这个问题?

//码////

  NSArray *list =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; NSLog(@"Response data: %@", responseString); NSLog(@"List ARRAY: %@", list); NSDictionary *dict = [list objectAtIndex: 0]; vehicleList = [dict objectForKey: @"assets"]; self.vehicleGroups = [[NSMutableArray alloc] init]; vehicleGroups = [dict objectForKey:@"identity"]; NSLog(@"Vehicle Groups: %@", vehicleGroups); NSString *identity = [dict objectForKey: @"identity"]; NSString *systemid = [dict objectForKey:@"systemId"]; self.listVehicles = [[NSMutableArray alloc] init]; self.listVehiclesID =[[NSMutableArray alloc]init]; NSLog(@"GroupOfVehicles: %@", groupOfVehicles); for (NSUInteger index = 0; index < [vehicleList count]; index++) { itemDict = [vehicleList objectAtIndex: index]; [self.listVehicles addObject:[itemDict objectForKey:@"identity"]]; [self.listVehiclesID addObject:[itemDict objectForKey:@"systemId"]]; } NSLog(@"Group Name: %@", identity); NSLog(@"Assets: %@", listVehicles); NSLog(@"Assets System ID: %@", listVehiclesID); NSLog(@"GroupSystemID: %@", systemid); 

你必须先初始化你的数组

 NSDict *list =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; NSLog(@"List Dict: %@", list); NSMutableArray *temp = list[@"assets"]; NSMutableArray *vehicleGroups = [NSMutableArry array]; vehicleGroups = temp[0][@"identity"]; NSLog(@"Vehicle Groups: %@", vehicleGroups); 

回答我自己的问题引导别人,如果他们面临同样的问题,其实我在这里做了什么来获得所需的身份数组:DEMO如下:

 vehicleGroups =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; NSLog(@"Response data: %@", responseString); NSLog(@"List ARRAY: %@", vehicleGroups); NSDictionary *dict1 = [vehicleGroups objectAtIndex: 0]; NSString *identity1 = [dict objectForKey: @"identity"]; NSLog(@"dictNEW: %@", dict1); groupOfVehicles = [[NSMutableArray alloc] init]; for (NSUInteger index = 0; index < [vehicleGroups count]; index++) { itemDict = [vehicleGroups objectAtIndex: index]; [groupOfVehicles addObject:[itemDict objectForKey:@"identity"]]; } NSLog(@"Group Name NEW: %@", identity1); NSLog(@"Groups NEW: %@", groupOfVehicles); 

使用该代码后,我已经正确地获得了我所需的数据的数组

保留数组为

[vehicleGroups retain] after vehicleGroups = [dict objectForKey:@"identity"];

 NSDictionary *dicts = [list objectAtIndex: 0]; NSMutableArray *vehicleGroups = [[NSMutableArray alloc] init]; for (NSDictionary *dict in dicts) { NSString* identity = [dict objectForKey:@"identity"]; [vehicleGroups addObject:identity]; } NSLog("count is %d",[vehicleGroups count]); 

试试 :)

您的JSON数据不是JSON 。

 { "assets" : [ { "identity" : "34DL3611", "systemId" : 544507 }, .... { "identity" = "34YCJ15", "systemId" = 294151155 } ], "identity" = "DEMO", "systemId" = 4921244 } 

这是您在JSON中描述的数据的一个可能的例子。

 NSDictionary *dict = [list objectAtIndex: 0]; NSMutableArray *vehicleGroups = [dict objectForKey:@"identity"]; 

它看起来像dict这里是一个字典有三个条目:资产,身份和systemId。 在这个级别,身份的价值只是一个string,“DEMO”,但是你正试图把它分配给一个可变数组。 我想你想要这样的东西,而不是:

 NSDictionary *dict = [list objectAtIndex: 0]; NSArray *assets = [dict objectForKey:@"assets"]; NSArray *vehicleGroups = [assets objectForKey:@"identity"]; 

(可以将-objectForKey:发送给一个数组 – 返回一个对应于接收器中每个对象的给定键的对象数组。