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.