Skip to content

Instantly share code, notes, and snippets.

@romyilano
Created February 9, 2013 05:24
Show Gist options
  • Select an option

  • Save romyilano/4743988 to your computer and use it in GitHub Desktop.

Select an option

Save romyilano/4743988 to your computer and use it in GitHub Desktop.
Thanks to one of the raywenderlich.com authors -> here's a nice NSDIctionary category extension that easily turns json data into convenient dictionaries toJSON: can be used on an NSDIctionary instance to get JSON data
// extend NSdictionary (foundation) class
@interface NSDictionary(JSONCategories)
+(NSDictionary *)dictionaryWithContentsOfJSONURLString:(NSString *)urlAddress;
-(NSData *)toJSON;
@end
@implementation NSDictionary(JSONCategories)
+(NSDictionary *)dictionaryWithContentsOfJSONURLString:(NSString *)urlAddress
{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlAddress]];
__autoreleasing NSError *error = nil;
id result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if (error !=nil) return nil;
return result;
}
// use this to call on an NSDictionary instnace to get JSON Data out of it
-(NSData *)toJSON
{
NSError *error = nil;
id result = [NSJSONSerialization dataWithJSONObject:self options:kNilOptions error:&error];
if(error !=nil ) return nil;
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment