2012-03-16 131 views
0

我剛開始爲IOS5編寫應用程序。我目前有一個PHP webservice,應用程序調用來從數據庫中檢索數據。在數據庫中我有3行,當我進入調試器時,字典有3個鍵/值對。我無法檢索任何值。我下面的代碼:無法從填充了JSON數據的NSDictionary中提取數據

NSURL *myUrl = [[NSURL alloc]initWithString:@"http://localhost/~stephen-hill9/index.php"]; 
NSData *data = [[NSData alloc] initWithContentsOfURL:myUrl]; 
NSError *error; 
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 
NSLog(@"Parsed Data: %@", json); 
NSDictionary *bodyDictionary = [json objectForKey:@"Product_Desc"]; <-APP CRASHES HERE - INVALID KEY 
NSLog(@"Parsed Data: %@", bodyDictionary); 

而且,這裏的數據字典中包含這是我從調試了:

Parsed Data: (
     { 
     0 = 1; 
     1 = "Caromax 20HN B/F"; 
     2 = 51533; 
     3 = 6650; 
     4 = 1117760; 
     5 = 1067224; 
     6 = 1759741; 
     7 = 1; 
     8 = "0000-00-00 00:00:00"; 
     ID = 1; 
     "Max_Volume" = 1759741; 
     "Product_Code" = 51533; 
     "Product_Desc" = "Caromax 20HN B/F"; 
     Pumpable = 1067224; 
     Status = 1; 
     "Tank_Level" = 6650; 
     Updated = "0000-00-00 00:00:00"; 
     Volume = 1117760; 
    }, 
     { 
     0 = 2; 
     1 = "C1 Kerosine"; 
     2 = 25101; 
     3 = 1258; 
     4 = 146871; 
     5 = 111776; 
     6 = 1018590; 
     7 = 2; 
     8 = "2023-02-12 20:00:00"; 
     ID = 2; 
     "Max_Volume" = 1018590; 
     "Product_Code" = 25101; 
     "Product_Desc" = "C1 Kerosine"; 
     Pumpable = 111776; 
     Status = 2; 
     "Tank_Level" = 1258; 
     Updated = "2023-02-12 20:00:00"; 
     Volume = 146871; 
    }, 
     { 
     0 = 3; 
     1 = "Recovered Spirit"; 
     2 = 87902; 
     3 = 6069; 
     4 = 710172; 
     5 = 675078; 
     6 = 1018200; 
     7 = 1; 
     8 = "2012-03-16 00:00:00"; 
     ID = 3; 
     "Max_Volume" = 1018200; 
     "Product_Code" = 87902; 
     "Product_Desc" = "Recovered Spirit"; 
     Pumpable = 675078; 
     Status = 1; 
     "Tank_Level" = 6069; 
     Updated = "2012-03-16 00:00:00"; 
     Volume = 710172; 
    } 
) 

我希望有人能夠幫助 - 我相信這個問題一定要基本的東西。

提前許多感謝,

斯蒂芬

+0

你怎麼從你的詞典中提取數據? – giorashc 2012-03-16 10:59:14

回答

1

好吧,你有什麼已經在這裏得到的是字典的數組,而不是一個字典本身。 試試這個:

NSArray *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 
NSDictionary *bodyDictionary = [json objectAtIndex:0]; 

當然,這樣只會讓你的第一個對象,但你的想法...

+0

謝謝你的回答 - 我無法找到NSDictionary的「objectAtIndex」方法,雖然:-S – HillInHarwich 2012-03-16 11:12:31

+0

糟糕...道歉 - 我只是將第一個字典--JSON更改爲一個數組,並將objectAtIndex提取到第二個字典,它已經工作。非常感謝您的幫助! – HillInHarwich 2012-03-16 11:17:55

+0

很高興幫助你:)考慮設置答案爲正確的,如果這是你在找什麼。 – Alladinian 2012-03-16 11:27:58