NSJSONReadingOptions - NSJSONReadingAllowFragments
Options used when creating Foundation objects from JSON data—see
JSONObjectWithData: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];