unrecognized selector error with NSJSONSerializati

2019-02-17 15:21发布

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

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.

It crashes on this line:

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

Error Log:

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'

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

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

The NSLog of the data looks like this:

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

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

1条回答
劫难
2楼-- · 2019-02-17 15:31

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

you could simply use

NSMutableArray *jsonArray = [NSMutableArray arrayWithArray:jsonData];

or, if it does not have to be mutable:

NSArray *jsonArray = (NSArray *) jsonData;

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.

查看更多
登录 后发表回答