2010-12-12 19 views
1

私はこれを自分自身で解決しようとしていました(私はすべての最後の夜は目を覚ましていました)。iPhone JSON - NSArrayを反復して値を取得する

私はこのチュートリアルを使用してJSONデータソース(http://www.mobileorchard.com/tutorial-jsonover-http-on-the-iphone/)に接続しましたが、うまくいきましたが、今は好きです値を取得する(私が考えるオブジェクトキーによって)。

私のjsonデータは以下の通りです: [{"Id": "1"、 "Name": "Richard"、 "NameOfFile": "1.jpg"}、{"Id": "395" "Name": "Alex"、 "NameOfFile": "390.jpg"}]

そして私が接続するために使用しているコードは次のとおりです。

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    [connection release]; 

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
    [responseData release]; 

    SBJSON *json = [[SBJSON new] autorelease]; 
    NSArray *myArray = [json objectWithString:responseString error:nil]; 
    [responseString release]; 

    for (int i = 0; i < [myArray count]; i++) 
    { 
     for(int colIndex = 0; colIndex < 5; colIndex++) 
     { 
      UIImage * myimage = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: @"http://www.goweruk.com/Images/Uploaded/220/873.jpg"]]]; 
      UIImageView *myimageview = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,50.0f, 50.0f)]; 
      [myimageview setImage:myimage]; 

      CGRect frameRect = myimageview.frame; 
      frameRect.origin.x = (frameRect.size.width + 14) * colIndex; 
      frameRect.origin.y = (frameRect.size.height + 14) * i; 
      myimageview.frame = frameRect; 

      [self.view addSubview:myimageview]; 
      [myimageview release]; 
     } 
    } 
} 

私のやりたいことは、そのループの中で項目値を取得することです。誰が私がそれをどうやってやるか知っていますか?あなたはまずfor..inを使用して辞書を反復処理、objectAtIndex:を用いて所望のインデックスで辞書を取得する必要がありますのでNSArrayは、NSDictionaryオブジェクトの配列であることを

答えて

8

for (int i = 0; i < [myArray count]; i++) 
{ 
    NSDictionary *dict = [myArray objectAtIndex:i]; 
    for(NSString *key in dict) { 
     //do something with key or [dict objectForKey:key]; 
    } 
} 
+0

私が探していたものです。私はそれを修正しました - 私は使用しました NSDictionary * dict = [myArray objectAtIndex:i]; NSLog([dict objectForKey:@ "NameOfFile"]); – tmutton

関連する問題