Ios NSJSONSerialization+;出现无法识别的选择器错误;AFN网络
更新:我刚刚使用测试了从服务器返回的JSON格式,没有问题 在对返回JSON数据的php脚本的AFNetworking调用中,NSJSONSerialization出现异常。我已经看了其他问题,这里有相同的问题,并尝试了这些解决方案,但仍然得到错误 它在这一行崩溃:Ios NSJSONSerialization+;出现无法识别的选择器错误;AFN网络,ios,json,ios5,afnetworking,Ios,Json,Ios5,Afnetworking,更新:我刚刚使用测试了从服务器返回的JSON格式,没有问题 在对返回JSON数据的php脚本的AFNetworking调用中,NSJSONSerialization出现异常。我已经看了其他问题,这里有相同的问题,并尝试了这些解决方案,但仍然得到错误 它在这一行崩溃: NSError *e = nil; NSMutableArray *jsonArray = [NSJSONSerialization JSONObjectWithData: jsonData
NSError *e = nil;
NSMutableArray *jsonArray =
[NSJSONSerialization JSONObjectWithData: jsonData
options: NSJSONReadingMutableContainers
error: &e];
错误日志:
NSError *e = nil;
NSMutableArray *jsonArray =
[NSJSONSerialization JSONObjectWithData: jsonData
options: NSJSONReadingMutableContainers
error: &e];
2012-03-19 18:10:41.291 imageUploader[3538:207]*由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[\uu NSCFArray bytes]:未识别的选择器发送到实例0x6867430'
当我通过浏览器调用php脚本时,我的JSON数据如下所示:
[{“用户”:“binky”,“路径”:“binky-0a96f9aab5267c8.jpg”,“索引”:“101”},{“用户”:“binky”,“路径”:“binky-9cf844252c28553.jpg”,“索引”:“102”},{“用户”:“binky”,“路径”:“binky-d6c749d25d33015.jpg”,“索引”:“103”]
数据的NSLog如下所示:
(
{
指数=101;
path=“binky-0a96f9aab5267c8.jpg”;
用户=宾基;
},
{
指数=102;
path=“binky-9cf844252c28553.jpg”;
用户=宾基;
},
{
指数=103;
path=“binky-d6c749d25d33015.jpg”;
用户=宾基;
})
最后,我做了一个测试以确保我有有效的JSON数据:
if ([NSJSONSerialization isValidJSONObject: jsonData]){
NSLog(@"Good JSON \n");
}
所以我不知道我的错误来源在哪里。帮个小忙
//网络操作+块
AFJSONRequestOperation *operation =
[AFJSONRequestOperation JSONRequestOperationWithRequest:myRequest
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id jsonData) {
NSLog(@"Success JSON data:\n %@ \n", jsonData); //log data
if ([NSJSONSerialization isValidJSONObject: jsonData]){
NSLog(@"Good JSON \n");
}
NSError *e = nil;
NSMutableArray *jsonArray = [NSJSONSerialization JSONObjectWithData: jsonData options: NSJSONReadingMutableContainers error: &e];
if (!jsonArray) {
NSLog(@"Error parsing JSON: %@", e);
} else {
for(NSDictionary *item in jsonArray) {
NSLog(@"Item: %@", item);
}
}
[self.navigationController popToRootViewControllerAnimated:YES];
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Error: %@", error);
[self.navigationController popToRootViewControllerAnimated:YES];
}];
JSONObjectWithData通常需要NSData而不是数组。 id jsonData似乎是一个表示json字符串内容的数组。很明显,您仍然需要一个数组 出于某种原因,你做了两次。而不是
NSMutableArray *jsonArray = [NSJSONSerialization JSONObjectWithData: jsonData options: NSJSONReadingMutableContainers error: &e];
你可以简单地使用
NSMutableArray *jsonArray = [NSMutableArray arrayWithArray:jsonData];
或者,如果它不一定是可变的:
NSArray *jsonArray = (NSArray *) jsonData;
但是,您应该始终测试它是否真的是jsonData中的数组。根据json字符串中的结构,它可以是NSDictionary,也可以是nil,以防出现错误 谢谢。将传入的jsondata转换为
NSMutableArray
是可行的,但是我不明白为什么我需要NSJSONSerialization
(它在许多与我类似的示例代码中使用)。