Sunday, May 12, 2013

NSJSONSerialization NSJSONWritingPrettyPrinted example ios


NSJSONWritingOptions - NSJSONWritingPrettyPrinted

Options for writing JSON data.
enum {     NSJSONWritingPrettyPrinted = (1UL << 0) }; typedef NSUInteger NSJSONWritingOptions;
Constants
NSJSONWritingPrettyPrinted
Specifies that the JSON data should be generated with whitespace designed to make the output more readable. If this option is not set, the most compact possible JSON representation is generated.


( NSJSONSerialization NSJSONWritingPrettyPrinted example )

NSDictionary *menuDictionary = [self dictionaryFromMenu:[[DataStore singleton] getHomeMenu]]; 

NSError *err;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:menuDictionary options:NSJSONWritingPrettyPrinted error:&err];

NSLog(@"JSON = %@", [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);

( NSJSONSerialization NSJSONWritingPrettyPrinted example )
NSArray *objects=[[NSArray alloc]initWithObjects:objects here,nil];
NSArray *keys=[[NSArray alloc]initWithObjects:corresponding keys of objects,nil];
NSDictionary *dict=[NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSData *jsonData=[NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
( NSJSONSerialization NSJSONWritingPrettyPrinted example )
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];
NSString *resultAsString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
 NSLog(@"jsonData as string:\n%@ Error:%@", resultAsString,error);