如何使用 NSJSONSerialization

我有一个 JSON 字符串(来自 PHP 的 json_encode(),如下所示:

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

我想把它解析成我的 iPhone 应用程序的某种数据结构。我想对我来说最好的事情是有一个字典数组,所以数组中的第0个元素是一个字典,键为 "id" => "1""name" => "Aaa"

我不明白 NSJSONSerialization是如何存储数据的,以下是我目前为止的代码:

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

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

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

225217 次浏览

您的根 json 对象不是一个 dictionary,而是一个数组:

[{"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);
}
}

你的代码看起来很好,除了结果是 NSArray而不是 NSDictionary,下面是一个例子:

前两行只是用 JSON 创建一个数据对象,与从网络读取数据对象相同。

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

对我有用。您的 data对象可能是 nil,正如 rckoene 所指出的,根对象应该是一个(可变的)数组。看这个代码:

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

(我必须用反斜杠转义 JSON 字符串中的引号。)

这是我用来检查接收到的 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 和 NSJSONReadingMutableContainer,它们都能正常工作。

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

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

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

您需要使用下面的代码来解析它:

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

@ rckoene 已经向您展示了如何正确地从 JSON 字符串获取数据。

对于您提出的问题: EXC_BAD_ACCESS几乎总是在对象[ auto-]释放后尝试访问它时出现。这并不是特定于 JSON [ de-]序列化,而只是与您获取一个对象并在它被释放后访问它有关。事实上,它是通过 JSON 实现的并不重要。

有很多很多的页面描述了如何调试这个程序——您希望使用 Google (或 SO) obj-c zombie objects,特别是 NSZombieEnabled,这对于帮助确定僵尸对象的源代码非常有价值。(“ Zombie”是当你释放一个对象,但是保留一个指向它的指针,并尝试在以后引用它的时候被称为“ Zombie”。)

下面的代码从 webserver 获取 JSON 对象,并将其解析为 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);
#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];
}

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

这个问题似乎与物体的自动释放有关。NSJSONSerialization JSONObjectWithData 显然是在创建一些自动发布的对象并将其传递回给您。如果您试图将它放到另一个线程上,那么它将无法工作,因为它不能在另一个线程上释放。

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

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

将 NSDictionary 视为 NSArray 不会导致糟糕的访问异常,而是可能在方法调用时崩溃。

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

注意: 对于 Swift 3 。 您的 JSON 字符串返回的是 Array 而不是 Dictionary:

        //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)
}

坏的例子,应该是这样的东西

{"id":1, "name":"something as name"}

数字和字符串是混合的。