2012-04-17 168 views
-1
-(int)getRatings:(int)idNo 
{ 

NSString *urlString = [NSString stringWithFormat:@"http://www.website.com/rating-grab.php"]; 
NSURL *url = [NSURL URLWithString:urlString]; 
NSData *data = [NSData dataWithContentsOfURL:url]; 
NSError *error; 
NSMutableArray *json = (NSMutableArray*)[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 



NSLog(@"json output \n: %@ \n",json); 
NSLog(@"______________\n」); 

/// not sure how to access the value 
NSLog(@"object at: %@\n",[json objectAtIndex:0]); 
} 

----- PHP內讀值平均行

// How to output values of more than one column at a time? 
$sth = mysql_query("SELECT AVG(overall) FROM votes WHERE idNo=1"); 

$rows = array(); 
while($r = mysql_fetch_assoc($sth)) { 
$rows[] = $r; 
} 
echo json_encode($rows); 

php output [{"AVG(overall)":"5.00000」}] 

Xcode output 

json output 
: (
     { 
     "AVG(overall)" = "5.00000"; 
    } 
) 
2012-04-17 15:54:12.202 

我希望能夠存儲每一列的平均作爲一個變量。我似乎正在接近,(當然不是最有效的方式)。

+0

我可以通過使用[[json objectAtIndex:0] objectForKey:@「AVG(overall)」]來獲取值;我想問題是......我是這樣做的低效率方式。如果可能,一次得到5列的平均值。 – 2012-04-17 23:31:58

回答

1

是否有理由不能修改您的SQL查詢以獲取同一請求中多列的平均值,例如, SELECT AVG(overall),AVG(othercolumn1),AVG(othercolumn2),AVG(othercolumn3) FROM ...

+0

沒有......只是學習和無知是一切。 – 2012-04-18 01:55:16