Skip to content

Instantly share code, notes, and snippets.

@tsbob
Created July 31, 2012 04:10
Show Gist options
  • Save tsbob/3213533 to your computer and use it in GitHub Desktop.
Save tsbob/3213533 to your computer and use it in GitHub Desktop.
NSDictionary with json
@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;
}
-(NSData*)toJSON
{
NSError* error = nil;
id result = [NSJSONSerialization dataWithJSONObject:self
options:kNilOptions error:&error];
if (error != nil) return nil;
return result;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment