1

I have a NSURLConnection working that the return can be a dictionary or an array How to know what is the kind of response Dictionary or array, so I do the appropriate serialisation?

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSError* error;
    NSDictionary* jsonDicto = [NSJSONSerialization
                               JSONObjectWithData:self.receivedData
                               options:kNilOptions
                               error:&error];

    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:self.receivedData
                                                         options:kNilOptions
                                                           error:&error];

}

Cheers .)

manuelBetancurt
  • 15,428
  • 33
  • 118
  • 216
  • 1
    You should first check whether you got a response whose content is JSON. You might get a response which indicates an error, and in this case servers often send html. That is, first check the status code if it equals 200 (OK) and also the MIME type of the response which should be `application/json`. – CouchDeveloper Jan 02 '14 at 10:24

1 Answers1

5

Use this:

id response = [NSJSONSerialization JSONObjectWithData:self.receivedData
                                                         options:kNilOptions
                                                           error:&error];

if([response isKindOfClass:[NSArray class]]) {
//Response is array
}
else if([response isKindOfClass:[NSDictionary class]]) {
//Reponse is Dictionary
}
Bhumeshwer katre
  • 4,671
  • 2
  • 19
  • 29