0

我有一个数组(称为结果):

@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,但我无法访问它们;有没有办法让我访问数组内每次迭代中的每个键?

4

2 回答 2

2
location.latitude = (double)[[[results objectAtIndex:i] objectForKey:lat] floatValue];
location.longitude = (double)[[[results objectAtIndex:i] objectForKey:long] floatValue];

希望这可以帮助...

myAnn.title = (double)[[[results objectAtIndex:i] objectForKey:title] floatValue];
myAnn.subtitle = (double)[[[results objectAtIndex:i] objectForKey:strap] floatValue];
于 2013-03-17T18:36:40.727 回答
1

当您提到“钥匙”一词时,我认为您的答案已经差不多了。

数组中的每个对象都是“”对象的可能性很高(我需要通过将代码放入测试项目来验证这一点),NSDictionary您可以从中获取“ lat”、“ long”、“ title”键的值。

于 2013-03-17T18:35:07.937 回答