2012-04-20 93 views
0

我在这里经历了类似的问题,但没有找到我面临的问题的答案。解析iOS中的JSON数据(Objective-c)并在TableView中显示。获取空值

我一直到现在都能够解析JSON数据并存储在字典中。这里的JSON数据看起来像原始形式:

{"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"}, 

等..

正如你所看到的,它是字典的数组的字典。因此,我首先将原始数据存储在字典中,将value提取为key = stores并将其存储在数组中。之后,我提取了每个字段并将其存储在自定义对象tempStore中。这是失败的时候。

- (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]); 
} 

这里的tempStore对象:

- (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; 
} 

现在,我使用populatedStoreArray用于填充表格的单元格。我不确定要显示的格式,但我主要关心的是当我尝试打印populatedStoreArray时,即使tempStore已填充,其内容仍然为空。 我在这里错过了什么? 此外,populateStoreArray作为属性在.h文件中声明。

@property (nonatomic, strong) NSMutableArray * populatedStoreArray; 

在此先感谢。

回答

0

可可事项已经说上面你需要确保你的populatedStoreArray被alloc'ed和初始化,Objective-C的不当您尝试将对象添加到nil数组时,出现错误,因此它看起来像添加它们,但不是。

另外我不知道它是否是你的实际代码,但我注意到你只有alloc并初始化tempStore一次。所以你循环访问数组并设置tempStore.address并每次添加同一个对象,所以你最终只能得到一个tempStore对象,你需要在每次迭代中分配并初始化一个新的tempStore对象你的循环:

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]); 
} 
2

请先ALLOC您的NSMutableArray还综合您的阵列的第一

populatedStoreArray = [[NSMutableArray alloc] init];