Shwethascar
Shwethascar

Reputation: 1142

Parsing JSON data in iOS (Objective-c) and displaying in TableView. Getting empty values

I went through similar questions here but did not find an answer to the issue I'm facing..

I have till now been able to parse JSON data and store in a dictionary. Here's how the JSON data looks like in its raw form:

{"stores":[{"address":"7801 Citrus Park Town Center Mall","city":"Tampa","name":"Macy's","latitude":"28.068052","zipcode":"33625","storeLogoURL":"http://strong-earth-32.heroku.com/images/macys.jpeg","phone":"813-926-7300","longitude":"-82.573301","storeID":"1234","state":"FL"},

{"address":"27001 US Highway 19N","city":"Clearwater","name":"Dillards's","latitude":"27.9898988","zipcode":"33761","storeLogoURL":"http://strong-earth-32.heroku.com/images/Dillards.jpeg","phone":"727-296-2242","longitude":"-82.7294986","storeID":"1235","state":"FL"},

and so on..

As you can see, it is a dictionary of an array of dictionaries. So, accordingly, I have first stored raw data in a dictionary, extracted the value for key = stores and stored that in an array. After that , I have extracted each field and stored it in a custom object tempStore. Here is when it fails.

- (void)viewDidLoad
{
[super viewDidLoad];
[populatedStoreArray addObject:@"blah"];
NSString *jsonRawData = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://strong-earth-32.heroku.com/stores.aspx"]];

if([jsonRawData length] == 0)
{
    [jsonRawData release];
    return;
}
SBJsonParser * parser = [[SBJsonParser alloc]init];
resultData = [[parser objectWithString:jsonRawData error:nil]copy];
NSArray *storeArray = [[NSArray alloc]init];
storeArray= [resultData objectForKey:@"stores"];
Store *tempStore = [[Store alloc]init];

/*NSLog(@"show me stores: %@", storeArray);*/
for(int i=1;i<[storeArray count];i++)
{
    NSDictionary *tempDictionary = [storeArray objectAtIndex:i];
    if([tempDictionary objectForKey:@"address"]!=nil)
    {
        tempStore.address= [tempDictionary objectForKey:@"address"];
        //NSLog(@"Address: %@",tempStore.address);
    }
    //and so on for other keys
    [populatedStoreArray addObject:tempStore]; 
    NSLog(@"In array: %@",[populatedStoreArray objectAtIndex:i]);
}

Here's the tempStore object:

- (id) init 
{
    if (self = [super init])
    {
    self.address = @"address";
    self.city = @"city";
    self.name = @"name";
    self.latitude = @"latitude";
    self.longitude = @"longitude";
    self.state = @"state";
    self.phone = @"phone";
    self.storeid = @"storeID";
    self.url = @"storeLogoURL";
    self.zipcode = @"zipcode";
    }
    return self;
}

Now, I use the populatedStoreArray for populating cells of the table. I'm not sure about the format to be displayed but my main concern is when I try to print populatedStoreArray, its contents are null even though tempStore has been populated. What am I missing here? Also, populatedStoreArray is declared in the .h file as a property.

@property (nonatomic, strong) NSMutableArray * populatedStoreArray;

Thanks in advance.

Upvotes: 0

Views: 2944

Answers (2)

Craig Mellon
Craig Mellon

Reputation: 5399

As Cocoa Matters has said above you need to ensure your populatedStoreArray is alloc'ed and initialised, objective-c does not error when you try to add objects to a nil array, so it looks like your adding them, but your not.

Also I don't know if its your actual code, but i noticed you only alloc and init the tempStore once. So you are looping through the array and setting the tempStore.address and adding the same object each time, so you will only ever end up with one tempStore object in the array, you need to alloc and init a new tempStore object in each iteration of your loop:

Store *tempStore;

/*NSLog(@"show me stores: %@", storeArray);*/
for(int i=1;i<[storeArray count];i++)
{
    tempStore = = [[Store alloc]init];

    NSDictionary *tempDictionary = [storeArray objectAtIndex:i];
    if([tempDictionary objectForKey:@"address"]!=nil)
    {
        tempStore.address= [tempDictionary objectForKey:@"address"];
        //NSLog(@"Address: %@",tempStore.address);
    }
    //and so on for other keys
    [populatedStoreArray addObject:tempStore]; 
    NSLog(@"In array: %@",[populatedStoreArray objectAtIndex:i]);
}

Upvotes: 0

Hiren
Hiren

Reputation: 12780

please first alloc your NSMutableArray also synthesize your array first

like populatedStoreArray = [[NSMutableArray alloc] init];

Upvotes: 2

Related Questions