JSON array for picker view iPhone

2020-02-15 02:20发布

问题:

I am getting this data from JSON web services

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;
})

By using this code:

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);

Here is the picker code I am using:

    -(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{
 }

The application crashes at the line

return [vehicleGroups count];

delegate method numberOfRowsInComponent of pickerView. I am not getting that - why I am facing this issue?

//Code////

    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);

回答1:

You have to initialise your array first

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);


回答2:

Answering my own question guide others if they face the same problem, Actually what I have done here to get the required array for identity: DEMO is as follows:

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);

After using that code I have properly got the array for my required data



回答3:

retain the array as

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



回答4:

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]);

have a try :)



回答5:

Your JSON data is not JSON.

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

This is a possible example of your data was it described in JSON.



回答6:

NSDictionary *dict = [list objectAtIndex: 0];

NSMutableArray *vehicleGroups = [dict objectForKey:@"identity"];

It looks to me like dict here is a dictionary with three entries: assets, identity, and systemId. At this level, the value of identity is just a string, "DEMO", but you're trying to assign it to a mutable array. I think you want something like this instead:

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

(It's okay to send -objectForKey: to an array -- you get back an array of objects corresponding to the given key for each object in the receiver.)