我有一个数组(称为结果):
@interface FourViewController : UIViewController
{
NSArray *results;
NSMutableData *data;
}
NSURL *url = [NSURL URLWithString:@"http://website/json.php"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
data = [[NSMutableData alloc] init];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
results = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
}
当我运行这个 for 循环时,它会正确返回数组中的所有键,其中每个 JSON 字符串位于:
int i;
for (i = 0; i < [results count]; i++) {
NSLog(@"Result: %i = %@", i, results[i]);
}
但是在 for 循环中我需要这样的东西,但我找不到文档来做到这一点:
int i;
for (i = 0; i < [results count]; i++) {
location.latitude = results[i][lat];
location.longitude = results[i][long];
myAnn.coordinate = location;
myAnn.title = results[i][title];
myAnn.subtitle = results[i][strap];
[locations addObject:myAnn];
}
lat、long、title 和 strap 都是 JSON 键/ID,但我无法访问它们;有没有办法让我访问数组内每次迭代中的每个键?