Last active
December 22, 2015 00:38
-
-
Save alvareztech/6390210 to your computer and use it in GitHub Desktop.
Parse JSON with Objective-C.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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