Convert JSON to NSArray

Brandon picture Brandon · Oct 11, 2014 · Viewed 13.5k times · Source

I have a JSON array being output on a page. I would like to convert this JSON array into an NSArray.

This is the JSON output on the web page:

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

This is my NSURL call and NSJSONSerialization:

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];

I would like to turn the NSDictionary called cityJSON into an NSArray. Can someone let me know what the next step might be? Thank you!

Answer

Fahim Parkar picture Fahim Parkar · Oct 11, 2014

How about this?

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