Convert JSON to NSArray

ⅰ亾dé卋堺 提交于 2019-12-09 06:07:40

问题


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!


回答1:


How about this?

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



回答2:


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



回答3:


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

Try printing

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



回答4:


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


来源:https://stackoverflow.com/questions/26311614/convert-json-to-nsarray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!