JSON array to select

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

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 selection code I'm 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{ } 

Line Application Failure

 return [vehicleGroups count]; 

delegate method numberOfRowsInComponent pickerView . I do not understand why I came across this problem?

//The 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); 
+2
source share
6 answers

When answering my own questions, guide others if they run into the same problem. In fact, what I did here to get the required array for identification: DEMO looks like this:

 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 this code, I got the array for the required data correctly

+2
source

You must initialize 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); 
+7
source

save array as

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

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

try :)

0
source

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 described in JSON.

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

It seems to me that dict is a dictionary with three entries: assets, identifier and systemId. At this level, the identifier value is just the "DEMO" string, but you are trying to assign it to a mutable array. I think you want something like this:

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

(It’s good to send -objectForKey: to the array - you return an array of objects corresponding to this key for each object in the receiver.)

0
source

Source: https://habr.com/ru/post/906667/


All Articles