Skip to content

Instantly share code, notes, and snippets.

@cemalyilmaz
Last active August 29, 2015 14:20
Show Gist options
  • Select an option

  • Save cemalyilmaz/7e0c0c6f35c31247ea48 to your computer and use it in GitHub Desktop.

Select an option

Save cemalyilmaz/7e0c0c6f35c31247ea48 to your computer and use it in GitHub Desktop.
NSDictionary to JSON String
#import <Foundation/Foundation.h>
@interface NSDictionary (MTJSON)
-(NSString*) mt_jsonString;
@end
#import "NSDictionary+MTJSON.h"
@implementation NSDictionary (MTJSON)
-(NSString*) mt_jsonString{
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self
options:NSJSONWritingPrettyPrinted
error:&error];
if (!jsonData) {
NSLog(@"JSON String Error: %@", error.localizedDescription);
return @"{}";
} else {
return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment