Skip to content
Jeff Hurray edited this page Jan 25, 2017 · 1 revision

#JSON Parsing in Objective-C

Step 1 get an object from the response

##Deserialization

NSError *jsonError = nil;
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&jsonError];

Note that this object can sometimes be of type NSArray. Its important to inspect a web service response to know what object type you are expecting.

##Access

Below is a mapping of JSON value types to the types they will be cast to in objective-c

String -> NSString *
int of float -> NSNumber *
list -> NSArray *
object -> NSDictionary *
null -> NSNull * (dont worry about this one, its an object representation of a null value)

###Dictionaries

// subscript notation
NSString *string = dictionary[@"key"];

// method notation
NSString *string = [dictionary objectForKey:@"key"];

###Arrays

// subscript notation
NSDictionary *dictionary = arrayOfDictionaries[0];

// method notation
NSDictionary *dictionary = [arrayOfDictionaries objectAtIndex:0];
Clone this wiki locally