用于选择器视图iPhone的JSON数组

Ome*_*han 2 iphone xcode json objective-c ios

我从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;
})
Run Code Online (Sandbox Code Playgroud)

通过使用此代码:

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);
Run Code Online (Sandbox Code Playgroud)

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

    -(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{
 }
Run Code Online (Sandbox Code Playgroud)

该应用程序在该行崩溃

return [vehicleGroups count];
Run Code Online (Sandbox Code Playgroud)

委托方法numberOfRowsInComponentpickerView.我没有得到 - 我为什么要面对这个问题?

//码////

    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);
Run Code Online (Sandbox Code Playgroud)

Hir*_*ren 7

您必须首先初始化您的阵列

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);
Run Code Online (Sandbox Code Playgroud)