Skip to content

Instantly share code, notes, and snippets.

@alvareztech
Last active December 22, 2015 00:38
Show Gist options
  • Save alvareztech/6390210 to your computer and use it in GitHub Desktop.
Save alvareztech/6390210 to your computer and use it in GitHub Desktop.
Parse JSON with Objective-C.
NSError *error = nil;
id jsonObject = [NSJSONSerialization
JSONObjectWithData:[string dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:&error];
if (jsonObject != nil && error == nil) {
NSLog(@"Successfully deserialized...");
if ([jsonObject isKindOfClass:[NSDictionary class]]) {
NSDictionary *deserializedDictionary = (NSDictionary *)jsonObject;
NSLog(@"Deserialized JSON Dictionary = %@", deserializedDictionary);
} else if ([jsonObject isKindOfClass:[NSArray class]]) {
NSArray *deserializedArray = (NSArray *)jsonObject;
NSLog(@"Deserialized JSON Array = %@", deserializedArray);
} else {
/* Some other object was returned. We don't know how to deal
with this situation, as the deserializer returns only dictionaries
or arrays */
}
} else {
if (error != nil) {
NSLog(@"An error happened while deserializing the JSON data.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment