我使用AFJSONRequestOperation
請求遠程API:解析JSON響應
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
//Remove the SVProgressHUD view
[SVProgressHUD dismiss];
//Check for the value returned from the server
NSData *jsonData = [JSON dataUsingEncoding:NSUTF8StringEncoding];//This line cause crash
NSArray *arr = [NSJSONSerialization JSONObjectWithData:jsonData
options:0
error:nil];
loginDic=[[NSDictionary alloc]init];
loginDic=[arr objectAtIndex:0];
NSLog(@"%@",loginDic);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Request Failed with Error: %@", [error.userInfo objectForKey:@"NSLocalizedDescription"]);
}];
[operation start];
[SVProgressHUD showWithStatus:@"Loading"];
然而,應用程序崩潰和我正在此錯誤:
[__NSCFDictionary dataUsingEncoding:]: unrecognized selector sent to instance
下面是JSON對象的NSLog
返回:
Result = (
{
operation = 5;
result = 1;
}
);
我是否錯過了一些東西,因爲我認爲我沒有解決問題g正確的JSON對象。請糾正我。