Showing posts with label NSJSONSerialization example. Show all posts
Showing posts with label NSJSONSerialization example. Show all posts

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);

NSJSONSerialization NSJSONReadingAllowFragments example ios


NSJSONReadingOptions - NSJSONReadingAllowFragments

Options used when creating Foundation objects from JSON data—seeJSONObjectWithData:options:error: and JSONObjectWithStream:options:error:.
enum {
   NSJSONReadingMutableContainers = (1UL << 0),
   NSJSONReadingMutableLeaves = (1UL << 1),
   NSJSONReadingAllowFragments = (1UL << 2)
};
typedef NSUInteger NSJSONReadingOptions;
Constants
NSJSONReadingMutableContainers
Specifies that arrays and dictionaries are created as mutable objects.
NSJSONReadingMutableLeaves
Specifies that leaf strings in the JSON object graph are created as instances of NSMutableString.
NSJSONReadingAllowFragments
Specifies that the parser should allow top-level objects that are not an instance of NSArray orNSDictionary.


( NSJSONSerialization NSJSONReadingAllowFragments example )

NSError *error = nil;
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
if (error) {
    NSLog(@"Error serializing %@", error);
}
NSLog(@"Dictionary %@", JSON);

( NSJSONSerialization NSJSONReadingAllowFragments example )
-(void)check{

    NSError *error = nil;
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.macscandal.com/?json=get_post&post_id=436"]];

    NSDictionary *dicData = [NSJSONSerialization
                             JSONObjectWithData:data
                             options:NSJSONReadingAllowFragments
                             error:&error];

    NSDictionary *postData = [dicData objectForKey:@"post"];
    NSString *title = [postData objectForKey:@"title"];

    NSLog(@"%@", title);
}
( NSJSONSerialization NSJSONReadingAllowFragments example )
// YOUR CODE
NSURL *jsonUrl = [NSURL URLWithString:@"http://website/Service1.svc/Names"];
NSError *error = nil;
NSData *jsonData = [NSData dataWithContentsOfURL:jsonUrl options:kNilOptions error:&error];
NSMutableDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error];
NSLog(@"%@",jsonResponse);

// MY ADDITIONS
NSArray *facultyMembers = [jsonResponse objectForKey:@"Faculty_Members"];
NSDictionary *facultyMember = [facultyMembers objectAtIndex:0];

NSJSONSerialization NSJSONReadingMutableLeaves example ios



NSJSONReadingOptions - NSJSONReadingMutableLeaves

Options used when creating Foundation objects from JSON data—seeJSONObjectWithData:options:error: and JSONObjectWithStream:options:error:.
enum {
   NSJSONReadingMutableContainers = (1UL << 0),
   NSJSONReadingMutableLeaves = (1UL << 1),
   NSJSONReadingAllowFragments = (1UL << 2)
};
typedef NSUInteger NSJSONReadingOptions;
Constants
NSJSONReadingMutableContainers
Specifies that arrays and dictionaries are created as mutable objects.
NSJSONReadingMutableLeaves
Specifies that leaf strings in the JSON object graph are created as instances of NSMutableString.
NSJSONReadingAllowFragments
Specifies that the parser should allow top-level objects that are not an instance of NSArray orNSDictionary.


( NSJSONSerialization NSJSONReadingMutableLeaves example )

NSError *myError = nil;

NSArray *jsonObjects= [NSJSONSerialization JSONObjectWithData:responseData   ptions:NSJSONReadingMutableLeaves error:&myError];


for (NSDictionary * dict in jsonObjects) {
    NSLog(@"Some data %@", [dict objectForKey:@"field"]);
    //replace this to access a valid field
  }

( NSJSONSerialization NSJSONReadingMutableLeaves example )
NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&myError];

if (!res) { // JSON parser failed }

// dictionary (top-level)
if (![res isKindOfClass:[NSDictionary class]]) {
    // JSON parser hasn't returned a dictionary
}

// sethostname (array of dictionaries)
NSArray *setHostNames = [res objectForKey:@"sethostname"];

// dictionary (array element)
for (NSDictionary *setHostName in setHostNames) {
    // status (number)
    NSNumber *status = [setHostName objectForKey:@"status"];

    // statusmsg (string)
    NSString *statusmsg = [setHostName objectForKey:@"statusmsg"];

    
}
( NSJSONSerialization NSJSONReadingMutableLeaves example )
NSError *error = nil;
   NSURL *request = [NSURL URLWithString:query];
   NSLog(@"Outgoing Request: %@",[request description]);
   NSString *response =  [NSString stringWithContentsOfURL:request encoding:NSUTF8StringEncoding error:nil];

   if (error) { 
       NSLog(@"ERROR: Unable to get response %@",[error localizedDescription]); 
   }

   NSLog("@Synchronous Response: %@",response);

   NSData *jsonData = [response dataUsingEncoding:NSUTF8StringEncoding];
   NSDictionary *results = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:&error];

   if (error) { 
       NSLog(@"ERROR: Unable to parse JSON %@",[error localizedDescription]); 
   }