本文介绍了在iOS(Objective-c)中解析JSON数据并在TableView中显示。获得空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我到现在为止能够解析JSON数据和存储在字典中。以下是JSON数据原始形式的样子:

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

等等..

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

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

这是tempStore对象:

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

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

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;

提前致谢。

推荐答案

正如Cocoa Matters上面所说,你需要确保你的populatedStoreArray被分配和初始化,当你尝试将对象添加到nil数组时,objective-c没有错误,所以看起来你的添加他们,但你没有。

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.

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

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

这篇关于在iOS(Objective-c)中解析JSON数据并在TableView中显示。获得空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-20 01:12