2012-04-22 57 views
0

我使用JSON解析來從MySql通過使用php獲取數據我得到了所有我想要的數據,但是當我想打印這些數據時,我創建了一個for循環,但它給了我最後一個元素只有..這是我的代碼檢索nsarray的所有數據

在DidViewLoad

 
- (void)viewDidLoad 
{ 
[super viewDidLoad];

NSString *phpUrl = @"http://dt-works.com/eman/bookOwn.php"; NSString *dbName = @"dbName"; NSString *localHost = @"localhost"; NSString *dbUser = @"dbUser"; NSString *dbPwd = @"dbPwd"; int u_id = 1; NSString *user_id = [NSString stringWithFormat:@"%d",u_id]; SBJsonParser *parser = [[SBJsonParser alloc] init]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:phpUrl]]; [request setHTTPMethod:@"POST"]; NSString *post = [[NSString alloc] initWithFormat:@"dbName=%@&localHost=%@&dbUser=%@&dbPwd=%@&user_id=%@&submit=", dbName, localHost, dbUser, dbPwd, user_id]; [request setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding]]; NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; NSArray *statuses = [parser objectWithString:json_string error:nil]; int arraySize = [statuses count]; for (NSDictionary *status in statuses) { bo_id2 = [status objectForKey:@"bo_id"]; bo_name2 = [status objectForKey:@"bo_name"]; bo_au_id2 = [status objectForKey:@"bo_au_id"]; bo_pub_id2 = [status objectForKey:@"bo_pub_id"]; bo_num_pages2 = [status objectForKey:@"bo_num_pages"]; bo_publish_year2 = [status objectForKey:@"bo_publish_year"]; } NSLog(@"size of array is: %d", arraySize); for (int i=0; i<arraySize; i++) { NSLog(@"%d:",i); // here I want to retrieve all array elements .. when I try NSLog(@"Book id: %@ - book name: %@", bookId, bookName); // this give me the last elemet only not all data // I want to get the data here to use it later .. how ??? }

}

任何幫助?

+0

您正在循環內打印一個變量。它會每次打印相同的值。這不是一個需要支持的問題。 – rakeshNS 2012-04-22 16:22:39

回答

0

我不明白爲什麼你必須使用兩個for循環來完成任務,在第一循環

NSMutableArray *books = [[NSMutableArray alloc] init]; 
for(NSDictionary *status in statuses){ 
    [books addObject:status]; 
} 

後來,當你想要檢索的任何書籍,你可以叫[books objectAtIndex:index]。 希望這可以幫助。 您應該能夠完成您要求的所有任務。

編輯 如果你只是想只有一個元素,bo_name例如,你可以使用:

NSMutableArray *books =[[NSMutableArray alloc] init]; 
for(NSDictionary *status in statuses){ 
    [books addObject:[status objectForKey:@"bo_name"]]; 
} 
+0

,因爲我想,所以我需要另一個用於循環再後來與其他東西使用它,當我使用它,它給我的最後一個元素只有數組的,我想所有的元素.. :( – Eman87 2012-04-22 15:21:08

+0

我的意思是,你可以打印時,在第一循環的元素,嘗試添加:的NSLog(@「書ID:%@」,[狀態objectForKey:@「bo_id」]);在第一循環,看看你是否能全部打印出來 – Jason 2012-04-22 15:25:20

+0

是它它們打印正常,但這並不是我的全部目標,我打印出來,以確保他們好不好,我要找回他們在另一條線以後使用.. – Eman87 2012-04-22 15:30:44

1

你似乎只是重新分配每個對象的值實例變量,實例變量只指向一個對象,而不是指定給它的所有對象。

+0

有什麼建議嗎? – Eman87 2012-04-22 15:26:03

+0

你想用數據做什麼? – Otium 2012-04-22 15:43:35

+0

我想檢索它,而不僅僅是最後一個元素,因爲我想將它插入到sqlite3表 – Eman87 2012-04-22 15:45:26

0

很可能你一定是在一些地方重組了陣列。檢查整個代碼。

0
NSLog(@"Book id: %@ - book name: %@", bookId, bookName); 

如何獲得上述NSLog中的bookId和bookName值?你的代碼中缺少一些東西。如果你想使用第二個for循環,走相同的方式,第一個

for(NSDictionary *status in statuses) { 
    NSLog(@"Book id: %@ - Book Name: %@", [status objectForKey:@"bo_id"], [status objectForKey: @"bo_name"]); 
} 

在其他方面,:

你可以試試這個。