6

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!

0

4 Answers 4

10

How about this?

cityArray = [[NSMutableArray alloc] init];
cityArray = [NSJSONSerialization JSONObjectWithData: cityData options:NSJSONReadingMutableContainers error:nil];
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1

JSONObjectWithData will return the NSArray if root object is array type.

Try printing

NSLog(@"%@", [cityJSON class]);

Comments

0

My Json Looks like

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

and my code is

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.