Brandon Brandon - 1 year ago 138
iOS Question

Convert JSON to NSArray

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 Source

How about this?

cityArray = [[NSMutableArray alloc] init];
cityArray = [NSJSONSerialization JSONObjectWithData: cityData options:NSJSONReadingMutableContainers error:nil];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download