0

我在这里遇到了类似的问题,但没有找到我面临的问题的答案..

到目前为止,我已经能够解析 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"},

等等..

如您所见,它是一个字典数组的字典。因此,我首先将原始数据存储在字典中,提取valueforkey = 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;
}

现在,我使用填充的StoreArray 来填充表格的单元格。我不确定要显示的格式,但我主要担心的是当我尝试打印populatedStoreArray 时,即使已填充tempStore,它的内容也是空的。我在这里想念什么?此外,populatedStoreArray 在 .h 文件中声明为属性。

@property (nonatomic, strong) NSMutableArray * populatedStoreArray;

提前致谢。

4

2 回答 2

2

请先分配你的NSMutableArray也先合成你的数组

populatedStoreArray = [[NSMutableArray alloc] init];

于 2012-04-20T07:26:22.920 回答
0

正如 Cocoa Matters 上面所说,您需要确保您的填充存储数组已分配和初始化,当您尝试将对象添加到 nil 数组时,objective-c 不会出错,因此看起来您添加了它们,但您没有。

另外我不知道它是否是您的实际代码,但我注意到您只分配和初始化 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]);
}
于 2012-04-20T08:23:09.477 回答