Skip to content

Instantly share code, notes, and snippets.

@mteece
Created August 31, 2015 19:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mteece/730d5732cb70cfc5e0c7 to your computer and use it in GitHub Desktop.
Save mteece/730d5732cb70cfc5e0c7 to your computer and use it in GitHub Desktop.
Deserialize JSON to Objective-C
{
"completed_in": 0.012,
"max_id": 136536013832069120,
"max_id_str": "136536013832069120",
"next_page": "?page=2&max_id=136536013832069120&q=twitterapi&rpp=1",
"page": 1,
"query": "twitterapi",
"refresh_url": "?since_id=136536013832069120&q=twitterapi",
"results": [
{
"created_at": "Tue, 15 Nov 2011 20:08:17 +0000",
"from_user": "fakekurrik",
"from_user_id": 370773112,
"from_user_id_str": "370773112",
"from_user_name": "fakekurrik",
"geo": null,
"id": 136536013832069120,
"id_str": "136536013832069120",
"iso_language_code": "en",
"metadata": {
"result_type": "recent"
},
"profile_image_url": "http://a1.twimg.com/profile_images/1540298033/phatkicks_normal.jpg",
"source": "<a href="http://twitter.com/">web</a>",
"text": "@twitterapi, keep on keeping it real",
"to_user": "twitterapi",
"to_user_id": 6253282,
"to_user_id_str": "6253282",
"to_user_name": "Twitter API"
}
],
"results_per_page": 1,
"since_id": 0,
"since_id_str": "0"
}
dictionary: {
"completed_in" = "0.012";
"max_id" = 136536013832069120;
"max_id_str" = 136536013832069120;
"next_page" = "?page=2&max_id=136536013832069120&q=twitterapi&rpp=1";
page = 1;
query = twitterapi;
"refresh_url" = "?since_id=136536013832069120&q=twitterapi";
results = (
{
"created_at" = "Tue, 15 Nov 2011 20:08:17 +0000";
"from_user" = fakekurrik;
"from_user_id" = 370773112;
"from_user_id_str" = 370773112;
"from_user_name" = fakekurrik;
geo = "<null>";
id = 136536013832069120;
"id_str" = 136536013832069120;
"iso_language_code" = en;
metadata = {
"result_type" = recent;
};
"profile_image_url" = "http://a1.twimg.com/profile_images/1540298033/phatkicks_normal.jpg";
source = "&lt;a href=&quot;http://twitter.com/&quot;&gt;web&lt;/a&gt;";
text = "@twitterapi, keep on keeping it real";
"to_user" = twitterapi;
"to_user_id" = 6253282;
"to_user_id_str" = 6253282;
"to_user_name" = "Twitter API";
}
);
"results_per_page" = 1;
"since_id" = 0;
"since_id_str" = 0;
}
// Read JSON from a file (simulate internet download)
NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"];
NSData *jsonData = [NSData dataWithContentsOfFile:path];
NSError *error = nil;
// Get JSON data into a Foundation object
id object = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error];
// Verify object retrieved is dictionary
if ([object isKindOfClass:[NSDictionary class]] && error == nil)
{
NSLog(@"dictionary: %@", object);
// Get the value (string) for key 'next_page'
NSString *str;
str = [object objectForKey:@"next_page"];
NSLog(@"next_page: %@", str);
// Get the value (an array) for key 'results'
NSArray *array;
// Get the 'results' array
if ([[object objectForKey:@"results"] isKindOfClass:[NSArray class]])
{
array = [object objectForKey:@"results"];
NSLog(@"results array: %@", array);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment