Created
July 31, 2012 04:10
-
-
Save tsbob/3213533 to your computer and use it in GitHub Desktop.
NSDictionary with json
This file contains hidden or 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
@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