2012-04-20 7 views
0

私はここで同様の質問をしましたが、私が直面している問題の答えを見つけられませんでした..iOS(Objective-c)でJSONデータを解析して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"}, 

など..

あなたが見ることができるように、それは辞書の配列の辞書です。したがって、私は最初に生データを辞書に格納し、key = storesvalueを抽出し、それを配列に格納しました。その後、私は各フィールドを抽出し、それをカスタムオブジェクトtempStoreに保存しました。ここが失敗するときです。私は、テーブルのセルを移入するためのpopulatedStoreArrayを使用し、今

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

- (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オブジェクトです。私は表示されるフォーマットについてはわかりませんが、私の主な関心事は、populatedStoreArrayを印刷しようとすると、tempStoreに値が設定されていてもその内容がnullであることです。 私はここで何が欠けていますか? また、populatedStoreArrayは.hファイル内でプロパティとして宣言されます。

@property (nonatomic, strong) NSMutableArray * populatedStoreArray; 

ありがとうございます。あなたのNSMutableArrayのもココア事項としてpopulatedStoreArray = [[NSMutableArray alloc] init];

答えて

0

のようなあなたの配列最初

を合成

2

してください最初のallocはあなたpopulatedStoreArrayがalloc'edと初期化されていることを確認する必要がある上に、Objective-Cのはないと述べていますnil配列にオブジェクトを追加しようとするとエラーが発生するので、追加するように見えますが、そうではありません。

また、あなたの実際のコードがわかりませんが、tempStoreを一度だけ割り当てて初期化することに気付きました。したがって、配列をループしてtempStore.addressを設定し、毎回同じオブジェクトを追加するので、配列内のtempStoreオブジェクトが1つしかないので、各繰り返しで新しい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]); 
} 
関連する問題