如何使用NSJSONSerialization

我有一个JSONstring(来自PHP的json_encode() ,看起来像这样:

 [{"id": "1", "name":"Aaa"}, {"id": "2", "name":"Bbb"}] 

我想parsing这个到我的iPhone应用程序的某种数据结构。 我想对我来说最好的办法是有一个字典数组,因此数组中的第0个元素是一个字典,其中键为"id" => "1""name" => "Aaa"

我不明白NSJSONSerialization如何存储数据。 这是我的代码到目前为止:

 NSError *e = nil; NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &e]; 

这只是我在其他网站上看到的一个例子。 我一直试图通过打印出这样的元素数量和类似的东西来读取JSON对象,但我总是得到EXC_BAD_ACCESS

我如何使用NSJSONSerializationparsing上面的JSON,并将其转换为我提到的数据结构?

你的根json对象不是一个字典,而是一个数组:

 [{"id": "1", "name":"Aaa"}, {"id": "2", "name":"Bbb"}] 

这可能会给你一个如何处理它的清晰画面:

 NSError *e = nil; NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &e]; if (!jsonArray) { NSLog(@"Error parsing JSON: %@", e); } else { for(NSDictionary *item in jsonArray) { NSLog(@"Item: %@", item); } } 

这是我的代码来检查收到的JSON是一个数组或字典:

 NSError *jsonError = nil; id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&jsonError]; if ([jsonObject isKindOfClass:[NSArray class]]) { NSLog(@"its an array!"); NSArray *jsonArray = (NSArray *)jsonObject; NSLog(@"jsonArray - %@",jsonArray); } else { NSLog(@"its probably a dictionary"); NSDictionary *jsonDictionary = (NSDictionary *)jsonObject; NSLog(@"jsonDictionary - %@",jsonDictionary); } 

我已经尝试了这个选项:kNilOptions和NSJSONReadingMutableContainers,并为两个正确的工作。

显然,实际的代码不能以这种方式在if-else块中创buildNSArray或NSDictionary指针。

它适用于我。 你的data对象可能是nil ,正如rckoenes所说,根对象应该是一个(可变的)数组。 看到这个代码:

 NSString *jsonString = @"[{\"id\": \"1\", \"name\":\"Aaa\"}, {\"id\": \"2\", \"name\":\"Bbb\"}]"; NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; NSError *e = nil; NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&e]; NSLog(@"%@", json); 

(我不得不用JSONstring中的反引号来反斜杠。)

你的代码似乎很好,除了结果是一个NSArray ,而不是一个NSDictionary ,这里是一个例子:

前两行只是用JSON创build一个数据对象,就像你从网上读取数据一样。

 NSString *jsonString = @"[{\"id\": \"1\", \"name\":\"Aaa\"}, {\"id\": \"2\", \"name\":\"Bbb\"}]"; NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; NSError *e; NSMutableArray *jsonList = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&e]; NSLog(@"jsonList: %@", jsonList); 

NSLog内容(字典列表):

 jsonList: ( { id = 1; name = Aaa; }, { id = 2; name = Bbb; } ) 
 [{"id": "1", "name":"Aaa"}, {"id": "2", "name":"Bbb"}] 

在上面的JSON数据中,您显示我们有一个包含字典数量的数组。

你需要使用这个代码来parsing它:

 NSError *e = nil; NSArray *JSONarray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &e]; for(int i=0;i<[JSONarray count];i++) { NSLog(@"%@",[[JSONarray objectAtIndex:i]objectForKey:@"id"]); NSLog(@"%@",[[JSONarray objectAtIndex:i]objectForKey:@"name"]); } 

对于迅捷3/3 +

  //Pass The response data & get the Array let jsonData = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [AnyObject] print(jsonData) // considering we are going to get array of dictionary from url for item in jsonData { let dictInfo = item as! [String:AnyObject] print(dictInfo["id"]) print(dictInfo["name"]) } 

以下代码从Web服务器获取JSON对象,并将其parsing为NSDictionary。 我已经使用openweathermap API来为这个例子返回一个简单的JSON响应。 为了简单起见,这段代码使用了同步请求。

  NSString *urlString = @"http://api.openweathermap.org/data/2.5/weather?q=London,uk"; // The Openweathermap JSON responder NSURL *url = [[NSURL alloc]initWithString:urlString]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; NSURLResponse *response; NSData *GETReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil]; NSDictionary *res = [NSJSONSerialization JSONObjectWithData:GETReply options:NSJSONReadingMutableLeaves|| NSJSONReadingMutableContainers error:nil]; Nslog(@"%@",res); 

@rckoenes已经向您展示了如何从JSONstring中正确地获取数据。

对于您所问的问题:当您尝试在[自动]释放后访问对象时,几乎总是会出现EXC_BAD_ACCESS 。 这不是JSON [de-]序列化特有的,而是与获取对象然后在被释放后访问对象有关。 它通过JSON来的事实并不重要。

有很多页面描述如何debugging这个 – 你要Google(或者SO) obj-c zombie objects ,尤其是NSZombieEnabled ,这对于帮助确定你的僵尸对象的来源将会是非常宝贵的。 (“僵尸”是你释放一个对象时所调用的东西,但是保留一个指针,并在稍后尝试引用它。)

Xcode 7(Beta)上的Swift 2.0与do / try / catch块:

 // MARK: NSURLConnectionDataDelegate func connectionDidFinishLoading(connection:NSURLConnection) { do { if let response:NSDictionary = try NSJSONSerialization.JSONObjectWithData(receivedData, options:NSJSONReadingOptions.MutableContainers) as? Dictionary<String, AnyObject> { print(response) } else { print("Failed...") } } catch let serializationError as NSError { print(serializationError) } } 

注意:对于Swift 3 。 您的JSONstring正在返回数组而不是字典。 请尝试以下内容:

  //Your JSON String to be parsed let jsonString = "[{\"id\": \"1\", \"name\":\"Aaa\"}, {\"id\": \"2\", \"name\":\"Bbb\"}]"; //Converting Json String to NSData let data = jsonString.data(using: .utf8) do { //Parsing data & get the Array let jsonData = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [AnyObject] //Print the whole array object print(jsonData) //Get the first object of the Array let firstPerson = jsonData[0] as! [String:Any] //Looping the (key,value) of first object for (key, value) in firstPerson { //Print the (key,value) print("\(key) - \(value) ") } } catch let error as NSError { //Print the error print(error) } 
 #import "homeViewController.h" #import "detailViewController.h" @interface homeViewController () @end @implementation homeViewController - (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.tableView.frame = CGRectMake(0, 20, 320, 548); self.title=@"Jason Assignment"; // Uncomment the following line to preserve selection between presentations. // self.clearsSelectionOnViewWillAppear = NO; // Uncomment the following line to display an Edit button in the navigation bar for this view controller. // self.navigationItem.rightBarButtonItem = self.editButtonItem; [self clientServerCommunication]; } -(void)clientServerCommunication { NSURL *url = [NSURL URLWithString:@"http://182.72.122.106/iphonetest/getTheData.php"]; NSURLRequest *req = [NSURLRequest requestWithURL:url]; NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:req delegate:self]; if (connection) { webData = [[NSMutableData alloc]init]; } } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [webData setLength:0]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [webData appendData:data]; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:webData options:0 error:nil]; /*Third party API NSString *respStr = [[NSString alloc]initWithData:webData encoding:NSUTF8StringEncoding]; SBJsonParser *objSBJson = [[SBJsonParser alloc]init]; NSDictionary *responseDict = [objSBJson objectWithString:respStr]; */ resultArray = [[NSArray alloc]initWithArray:[responseDict valueForKey:@"result"]]; NSLog(@"resultArray: %@",resultArray); [self.tableView reloadData]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { //#warning Potentially incomplete method implementation. // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { //#warning Incomplete method implementation. // Return the number of rows in the section. return [resultArray count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } // Configure the cell... cell.textLabel.text = [[resultArray objectAtIndex:indexPath.row] valueForKey:@"name"]; cell.detailTextLabel.text = [[resultArray objectAtIndex:indexPath.row] valueForKey:@"designation"]; NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[resultArray objectAtIndex:indexPath.row] valueForKey:@"image"]]]; cell.imageview.image = [UIImage imageWithData:imageData]; return cell; } /* // Override to support conditional editing of the table view. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { // Return NO if you do not want the specified item to be editable. return YES; } */ /* // Override to support editing the table view. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { // Delete the row from the data source [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; } else if (editingStyle == UITableViewCellEditingStyleInsert) { // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view } } */ /* // Override to support rearranging the table view. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { } */ /* // Override to support conditional rearranging of the table view. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { // Return NO if you do not want the item to be re-orderable. return YES; } */ #pragma mark - Table view delegate // In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // Navigation logic may go here, for example: //Create the next view controller. detailViewController *detailViewController1 = [[detailViewController alloc]initWithNibName:@"detailViewController" bundle:nil]; //detailViewController *detailViewController = [[detailViewController alloc] initWithNibName:@"detailViewController" bundle:nil]; // Pass the selected object to the new view controller. // Push the view controller. detailViewController1.nextDict = [[NSDictionary alloc]initWithDictionary:[resultArray objectAtIndex:indexPath.row]]; [self.navigationController pushViewController:detailViewController1 animated:YES]; // Pass the selected object to the new view controller. // Push the view controller. // [self.navigationController pushViewController:detailViewController animated:YES]; } @end - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. empName.text=[nextDict valueForKey:@"name"]; deptlbl.text=[nextDict valueForKey:@"department"]; designationLbl.text=[nextDict valueForKey:@"designation"]; idLbl.text=[nextDict valueForKey:@"id"]; salaryLbl.text=[nextDict valueForKey:@"salary"]; NSString *ImageURL = [nextDict valueForKey:@"image"]; NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]]; image.image = [UIImage imageWithData:imageData]; } 

这个问题似乎与autorelease的对象。 NSJSONSerialization JSONObjectWithData显然是创build一些自动发布的对象,并把它传回给你。 如果你试图把它带到另一个线程,它将无法工作,因为它不能在另一个线程上解除分配。

诀窍可能是尝试做该字典或数组的可变副本,并使用它。

 NSError *e = nil; id jsonObject = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &e] mutableCopy]; 

将NSDictionary作为NSArray处理不会导致Bad访问exception,但在进行方法调用时可能会崩溃。

另外,可能这些选项在这里并不重要,但最好给NSJSONReadingMutableContainers | NSJSONReadingMutableContainers | NSJSONReadingAllowFragments,但即使它们是自动发布的对象,它可能无法解决此问题。