Skip to content

Instantly share code, notes, and snippets.

@matths
Created January 10, 2013 10:04
Show Gist options
  • Save matths/4500902 to your computer and use it in GitHub Desktop.
Save matths/4500902 to your computer and use it in GitHub Desktop.
some basics using NSJSONSerialization (iOS5 and up.)
NSString *strArrayExample = @"%5B%22hello%22%2C%22world!%22%2C12%5D";
NSString *strObjectExample = @"%7B%22a%22%3A%221%22%2C%22b%22%3A%22c%22%7D";
NSString *str = [strArrayExample stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"str:: %@", str);
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"type of data:: %@", NSStringFromClass([json class]));
if ([json isKindOfClass:[NSArray class]]) {
NSLog(@"is array");
} else if ([json isKindOfClass:[NSDictionary class]]) {
NSLog(@"is object");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment