0

I'm developing an app that needs to send request to web service and obtain JSON for the next process. I'm using AFNetworking for the request. Following is my PHP code

$data = array(
            array(
                'userID' => 'xxx'
            )
        );
echo json_encode($data);

I've already verified the above code, and the result printed is fine.

[{"userID":"xxx"}]

Then, I use AFNetworking to send a request for this Json data as below

NSDictionary *parameters = @{@"action": @"verifyUDID", @"UDID": udid};

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init];

[manager POST:@"http://roommateradar.com/RoommateRadarAPI.php"  
         parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {

             NSLog(@"%@",responseObject);
             NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

The NSLog is correctly show the JSON data, but the next line which is parsing JSON to NSDictionary will cause a crash, the output is like below

2014-04-10 21:59:56.893 RoommateRadar[2941:60b] (
        {
        userID = xxx;
    }
)
2014-04-10 21:59:56.893 RoommateRadar[2941:60b] -[__NSCFArray bytes]: unrecognized selector sent to instance 0x1493e660
2014-04-10 21:59:56.895 RoommateRadar[2941:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray bytes]: unrecognized selector sent to instance 0x1493e660'

So you can see that the NSLog(@"%@",responseObject); is actually showing correct thing. BTW, I also tried

[NSJSONSerialization isValidJSONObject:responseObject];

to verify the responseObject, the result is also valid.

Any little help?

1
  • Because you are getting array from service so get NSArray *arr = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil]; and then get NSDictionary *dictT = [arr objectAtIndex:0]; and then do NSLog("%@",dictT). Commented Apr 10, 2014 at 12:09

1 Answer 1

2

AFNetworking already deserialises the responseObject for you, so you don't need to use NSJSONSerialization.

The crash is because you are passing the deserialised NSArray to NSJSONSerialization when it expects NSData.

Also, when you do:

NSDictionary *dictionary = [NSJSONSerialization ...

you would not get a dictionary anyway, you would get an array. You should just have:

NSArray *users = (NSArray *)responseObject;
NSDictionary *user = [users objectAtIndex:0];

(obviously you need to check the users array count)

Also, when you use isValidJSONObject:, you're using it the wrong way round. It verifies if an object can be converted into JSON, not if the object can be converted from JSON.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! You are right, AFNetworking has already deserialized the JSON for me. Now I can get the userID from responseObject. However, When I try to get the userID from the NSArray users like [users objectAtIndex:0], it cause a crash again. Instead, I use NSDictionary *users = [responseObject objectAtIndex:0] and then it works for '[users objectForKey:@"userID"]`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.