NSJSONSerialization + AFNetworking无法识别的选择器错误 [英] unrecognized selector error with NSJSONSerialization + AFNetworking

查看:98
本文介绍了NSJSONSerialization + AFNetworking无法识别的选择器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:我刚刚使用 JSONlint 测试了从服务器返回的JSON格式,这很好。

Update: I just tested my JSON format returned from the server using JSONlint and it is fine.

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

I'm getting an exception with NSJSONSerialization in an AFNetworking call to a php script that returns JSON data. I've looked at the other questions here with the same issue and tried those solutions but am still getting the error.

它崩溃了这一行:

 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'

2012-03-19 18:10:41.291 imageUploader[3538:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray bytes]: unrecognized selector sent to instance 0x6867430'

我的JSON数据,当我通过浏览器调用php脚本时看起来像这样:

My JSON data, when I call the php script through the browser looks like this:


[{user:binky,path:binky-0a96f9aab5267c8.jpg,index: 101 },{ 用户 : 宾基 路径: 宾基-9cf844252c28553.jpg, 索引: 102},{ 用户: 宾基, 路径: binky- d6c749d25d33015.jpg,index:103}]

[{"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如下所示:

The NSLog of the data looks like this:



{
index = 101;
path =binky-0a96f9aab5267c8.jpg;
user = binky;
},
{
index = 102;
path =bink的y 9cf844252c28553.jpg;
user = binky;
},
{
index = 103;
path =binky-d6c749d25d33015.jpg;
user = binky;
})

( { index = 101; path = "binky-0a96f9aab5267c8.jpg"; user = binky; }, { index = 102; path = "binky-9cf844252c28553.jpg"; user = binky; }, { index = 103; path = "binky-d6c749d25d33015.jpg"; user = binky; } )

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

Finally, I do a test to make sure I have valid JSON data:

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

所以我无法理解错误来源的位置。帮助不大?

So I can't understand where the source of my error is. Little help?

// AFNetworking操作+块

// AFNetworking operation + block

    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字符串内容的数组。很明显,无论如何你都期待一个阵列。

Aparently JSONObjectWithData expects NSData rather than an array. id jsonData seems to be an Array representing the content of your json string. Aparently you are expecting an Array anyway.

出于某种原因,你要做两次。而不是

For some reason you are doing it twice. Instead of

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

你可以简单地使用

NSMutableArray *jsonArray = [NSMutableArray arrayWithArray:jsonData];

或者,如果它不必是可变的:

or, if it does not have to be mutable:

NSArray *jsonArray = (NSArray *) jsonData;

但是,您应该始终测试它是否真的是jsonData中的数组。根据json字符串中的结构,如果出现错误,它可能是NSDictionary或nil。

However, you should always test whether it is really an array in jsonData. Depending on the structure within the json string it could be an NSDictionary or nil in case of errors.

这篇关于NSJSONSerialization + AFNetworking无法识别的选择器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆