2012-03-01 12 views
0

私はIPOD Appを作成しており、JSONフィードから作成したNSDictionaryを持っています。私はこれをNSMutableArrayに変換しようとしています。私は辞書をループしてキーと値を配列に書く必要があることを知っていますが、コードに問題があります。これはこれを達成するためのものです。iOS 5 NSDictionaryをNSMutableArrayに変換する際の問題?

////ウェブサイト NSError *エラーからJSONデータを取得します。 NSDictionaryの* JSON = [NSJSONSerialization JSONObjectWithData:データオプション:kNilOptionsエラー:&エラー]。事前に

////// convert the dictionary to an NSMutableArray 
// create an empty array of size equal to the number of json records 
NSMutableArray *users = [[NSMutableArray alloc] initWithCapacity:1];       
// Begin filling the array 
NSInteger dCount = [json count]; 

for(int i=0; i < dCount; i++) 
{ 
    User *user = [[User alloc] init]; 
    user.emailAddress = [json objectForKey: @"emailAddress"]; 
    user.password = [json objectForKey: @"password"]; 
    user.password = [json objectForKey: @"password"]; 
    [users addObject:user]; 
    NSLog(@"This is record: %@", i); 

}

感謝。

答えて

0

オーケーは最もエレガントな解決策ではないかもしれないが、多くの助けでこれは私が思い付いたものです:

////////////////の配列を構築しますサービス上のユーザ dispatch_queue_t bgQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT、0); dispatch_async(bgQueue、^ {

////// Building users list 
//Get the data from the webserver 
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://msis-discussion-forum.appspot.com/users"]]; 

NSLog(@"Beginning to build array"); 
//// Get the JSON data from the website 
NSError* error; 
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 
NSLog(@"This is my JSON data %@", json); 
usersArray =[[NSMutableArray alloc]init]; 
NSArray *userJSONObjects = [json objectForKey:@"users"]; 
for (User *tempUser in userJSONObjects) 
{ 
    User *user = [[User alloc] init]; 
    user.displayName = [tempUser valueForKey:@"displayName"]; 
    NSLog(@"%@",user.displayName); 
    user.emailAddress = [tempUser valueForKey:@"emailAddress"]; 
    NSLog(@"%@",user.emailAddress); 
    user.password = [tempUser valueForKey: @"password"]; 
    NSLog(@"%@",user.password); 
    [usersArray addObject:user]; 
} 
}); 

はそれがお役に立てば幸いです。

関連する問題