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