0

I am using this code to get the values of JSON,

    NSDictionary *dict = [list objectAtIndex: 0];

vehicleList = [dict objectForKey: @"assets"];

NSString *identity = [dict objectForKey: @"identity"];

for (NSUInteger index = 0; index < [vehicleList count]; index++) {

    itemDict = [vehicleList objectAtIndex: index];

    NSMutableArray *listVehicles = [itemDict objectForKey: @"identity"];

    NSLog(@"Assets: %@", listVehicles);
}

it is working properly and showing exact results

Assets: 34DL3611
Assets: 34GF0512
Assets: 34HH1734
Assets: 34HH1736
Assets: 34YCJ15

I want to use these values to populate an array so that I would use that array for tableview.

2 Answers 2

3

Make your listVehicles a NSMutableArray and do [listVehicles addObject:[itemDict objectForKey:@"identity"]] instead of what you are currently doing inside your loop.

Sign up to request clarification or add additional context in comments.

3 Comments

you haven't done it right. Create an array first and add the object properly. Your new fix (I suppose) NSMutableArray *listVehicles = [itemDict objectForKey: @"identity"]; isn't what I've told you .
no I used this as you told me [listVehicles addObject:[itemDict objectForKey:@"identity"]];
listVehicles = [[NSMutableArray alloc] init]; outside your loop. And then [listVehicles addObject:[itemDict objectForKey:@"identity"]]; inside your loop.
1

If I'm reading what you're saying correctly I would suggest creating your for loop slightly differently to increase readability and streamline the method.

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


NSMutableArray * listVehicles = [NSMutableArray arrayWithCapacity:vehicles.count];


for (NSDictionary * currentDictionary in vehicles)
{
    NSString * identity = [currentDictionary objectForKey:@"identity"];

    [listVehicles addObject:identity];
    NSLog(@"Added %@ to the array, which is now contains %d objects", identity, listVehicles.count);
}

This is a nice built in feature that keeps you from needing to deal with counts and calling objectForIndexAt:

I just quickly typed that up, but I'm pretty sure it does what you're doing. If not though, I still encourage you to tweak it and use that iteration style, I think you'll like it.

2 Comments

thanx for this information Ryan Crews ... can't accept two answers for 1 question but voting it up.. :)
No worries, the actual answer is the same, just wanted to share the iteration tip =]

Your Answer

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