2

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);
6
  • There is nothing in log, it gives me a message: Program received signal: EXC_BAD_ACCESS Commented Jan 17, 2012 at 7:07
  • show the code for the picker and also mention where the app crashes Commented Jan 17, 2012 at 7:15
  • I have edited my question and added the code for pickerView Commented Jan 17, 2012 at 7:22
  • put a breakpoint over there and check if it takes value for vehicleGroups or not. Commented Jan 17, 2012 at 7:27
  • @hiren443 response is getting properly I am already getting the data for assets array and it is being used in a table, I think the problem is that I am getting the string not the array ... Commented Jan 17, 2012 at 7:30

6 Answers 6

7

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);
Sign up to request clarification or add additional context in comments.

8 Comments

have initialized it now, but the problem is still there
then NSLog your dictionary and check it its shown or nil also tyr first NSLog your dict
you need all the identity in your vehicleGroups?
List ARRAY: ( {assets = ({ identity = 34DL3611; systemId = 544507;},{....}....); identity = DEMO; systemId = 4921244;} Here I m getting all the values for assets and identity (in the assets array), but the value for identity = DEMO which is in List Array should be shown in picker view, its not working
check my question again please I have edited it and added code
|
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

Comments

0

retain the array as

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

1 Comment

I have tried this now got this error on console: -[__NSCFString count]: unrecognized selector sent to instance
0
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 :)

1 Comment

got error [__NSCFString addObject:]: unrecognized selector sent to instance
0

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.

Comments

0
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.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.