Update: I just tested my JSON format returned from the server using JSONlint , and that's fine.
I get an exception from NSJSONSerialization in an AFNetworking call on a php script that returns JSON data. I looked at other issues here with the same problem and tried these solutions, but still getting the error.
Failure 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] * Application terminated due to an uncaught exception "NSInvalidArgumentException", reason: '- [__ NSCFArray bytes]: unrecognized selector sent to instance 0x6867430'
JSON, php script , :
[{ "" : "", "" : "Binky-0a96f9aab5267c8.jpg", "": "101" }, { "" : "", "" : "binky- 9cf844252c28553.jpg", "": "102" }, { "" : "", "" : "Binky-d6c749d25d33015.jpg", "": "103" }]
NSLog :
( { 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:
if ([NSJSONSerialization isValidJSONObject: jsonData]){
NSLog(@"Good JSON \n");
}
, . ?
// AFNetworking + block
AFJSONRequestOperation *operation =
[AFJSONRequestOperation JSONRequestOperationWithRequest:myRequest
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id jsonData) {
NSLog(@"Success JSON data:\n %@ \n", jsonData);
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];
}];