2014-10-11 11 views
5

Bir sayfada çıktı olarak bir JSON dizilim var. Bu JSON dizisini bir NSArray'e dönüştürmek istiyorum. Bu benim NSURL çağrısı ve NSJSONSerializationJSON - NSArray'a Dönüştür

[{"city":"Current Location"},{"city":"Anaheim, CA"},{"city":"Los Angeles, CA"},{"city":"San 
Diego, CA"},{"city":"San Francisco, CA"},{"city":"Indianapolis, IN"}] 

geçerli::

Bu

web sayfasında JSON çıkışı ben NSDictionary içine cityJSON denilen açmak istiyoruz

NSString *cityArrayURL = [NSString stringWithFormat:@"http://site/page.php"; 
NSData *cityData = [NSData dataWithContentsOfURL:[NSURL URLWithString:cityArrayURL]]; 

NSError *error; 
NSDictionary *cityJSON = [NSJSONSerialization JSONObjectWithData:cityData 
options:kNilOptions error:&error]; 

bir NSArray. Birisi bana bir sonraki adımın ne olabileceğini bildirebilir mi? Teşekkür ederim!

cevap

10

Bu nasıl? kök nesnesi dizi türü ise

cityArray = [[NSMutableArray alloc] init]; 
cityArray = [NSJSONSerialization JSONObjectWithData: cityData options:NSJSONReadingMutableContainers error:nil]; 
1

JSONObjectWithData NSArray dönecektir.

deneyin baskı

NSLog(@"%@", [cityJSON class]); 
3
NSString *requestString = @"http://pastebin.com/raw.php?i=iX5dZZt3"; 

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:requestString]]; 

NSError *error; 
NSDictionary *cityJSON = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 

NSArray *cityArray = [cityJSON valueForKey:@"city"]; 
NSLog(@"%@",cityArray); 

NSLog(@"Response is of type: %@", [cityArray class]); 
0

{"results":[{"state_name":"Ariyalur"},{"state_name":"Chennai"},{"state_name":"Coimbatore"},{"state_name":"Cuddalore"},{"state_name":"Dharmapuri"}}}]} 

ve benim kod

NSMutableArray pickerArray = [[NSMutableArray alloc]init]; 
     NSString *urlAsString = @"urlLink"; 
     NSURL *url = [[NSURL alloc] initWithString:urlAsString]; 
     NSLog(@"%@", urlAsString); 

     [NSURLConnection sendAsynchronousRequest:[[NSURLRequest alloc] initWithURL:url] queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 

      if (error) { 
       NSLog(@"%@",error.localizedDescription); 
      } else { 
       NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data 
                    options:NSJSONReadingMutableContainers 
                     error:&error]; 
       NSLog(@"Cites: %@", [json valueForKey:@"results"]); 

       NSMutableArray *rArray = [json valueForKey:@"results"]; 
       NSLog(@"%@",rArray); 

       for(int i=0; i<rArray.count ; i++) 
       { 
        NSDictionary *dict = [rArray objectAtIndex:i]; 
        [pickerArray addObject:[dict objectForKey:@"state_name"]]; 
       } 
       NSLog(@"The array is = %@", pickerArray); 
      } 
     }]; 
olduğu gibi My Json görünüyor