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の値を取得できる可能性が高いです(そして、コードをテストプロジェクトにドロップしてこれを確認する必要があります)。latlongtitle

于 2013-03-17T18:35:07.937 に答える