【问题标题】:unrecognized selector error with NSJSONSerialization + AFNetworkingNSJSONSerialization + AFNetworking 出现无法识别的选择器错误
【发布时间】:2012-04-04 10:49:13
【问题描述】:

更新:我刚刚使用JSONlint 测试了从服务器返回的 JSON 格式,没问题。

我在 AFNetworking 调用返回 JSON 数据的 php 脚本时遇到 NSJSONSerialization 异常。我已经在这里查看了具有相同问题的其他问题并尝试了这些解决方案,但仍然出现错误。

它在这一行崩溃:

 NSError *e = nil;
 NSMutableArray *jsonArray = 
 [NSJSONSerialization JSONObjectWithData: jsonData 
                                 options: NSJSONReadingMutableContainers 
                                   error: &e];

错误日志:

2012-03-19 18:10:41.291 imageUploader[3538:207] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[__NSCFArray bytes]:无法识别的选择器发送到实例 0x6867430'

当我通过浏览器调用 php 脚本时,我的 JSON 数据如下所示:

[{"user":"binky","path":"binky-0a96f9aab5267c8.jpg","index":"101"},{"user":"binky","path":"binky- 9cf844252c28553.jpg","index":"102"},{"user":"binky","path":"binky-d6c749d25d33015.jpg","index":"103"}]

数据的NSLog是这样的:

( { 指数 = 101; 路径=“binky-0a96f9aab5267c8.jpg”; 用户 = binky; }, { 指数 = 102; 路径 = "binky-9cf844252c28553.jpg"; 用户 = binky; }, { 指数 = 103; 路径=“binky-d6c749d25d33015.jpg”; 用户 = binky; })

最后,我做一个测试以确保我有有效的 JSON 数据:

         if ([NSJSONSerialization isValidJSONObject: jsonData]){
             NSLog(@"Good JSON \n");
         }

所以我不明白我的错误的根源在哪里。一点帮助?

// AFNetworking 操作 + 阻塞

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

【问题讨论】:

    标签: ios json ios5 afnetworking


    【解决方案1】:

    显然 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 在这里(它在很多示例代码中使用,就像我的一样)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-07
    相关资源
    最近更新 更多