Can't get NSArray of Custom Class to display on cellForRowAtIndexPath: - ios

All, I'm losing my mind. I have a class with the following code passing an NSArray of "Events" to my View Controller:
+ (NSArray *)eventsFromJSON:(NSData *)objectNotation error:(NSError **)error {
NSError *localError = nil;
NSArray *parsedObject = [NSJSONSerialization JSONObjectWithData:objectNotation options:0 error:&localError];
if (localError != nil) {
*error = localError;
return nil;
}
NSMutableArray *events = [[NSMutableArray alloc] init];
//NSArray *results = [parsedObject valueForKey:#"results"];
NSLog(#"Count %lu", (unsigned long)parsedObject.count);
for (NSDictionary *eventDic in parsedObject) {
Event *event = [[Event alloc] init];
for (NSString *key in eventDic) {
if ([event respondsToSelector:NSSelectorFromString(key)]) {
[event setValue:[eventDic valueForKey:key] forKey:key];
}
}
[events addObject:event];
}
return events;
}
On my view controller I've got the following code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"eventCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
Event *event = [self.eventData objectAtIndex:indexPath.row];
cell.textLabel.text = event.title;
cell.detailTextLabel.text = event.link;
return cell;
}
- (void) didReceiveEvents:(NSArray *)events {
self.eventData = [events copy];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}
A sample of the JSON I'm parsing is below:
[
{
"ID": 576,
"title": “Event Title“,
"status": "publish",
"type": "tribe_events",
"author": {
"ID": 1,
"username": "admin",
"name": "admin",
"first_name": “Name”,
"last_name": “Last”,
"nickname": "admin",
"slug": "admin",
"URL": "",
"avatar": "1.gravatar.com/avatar/",
"description": "",
"registered": "-001-11-30T00:00:00+00:00",
"meta": {
"links": {
"self": "url.com/wp-json/users/1",
"archives": "url.com/wp-json/users/1/posts"
}
}
},
"content": "<p>Join us if you are interested.</p>\n",
"parent": {
"ID": 575,
"title": “Title”,
"status": "publish",
"type": "tribe_events",
"author": {
"ID": 1,
"username": "admin",
"name": "admin",
"first_name": “First”,
"last_name": “Last”,
"nickname": "admin",
"slug": "admin",
"URL": "",
"avatar": "1.gravatar.com/avatar/?s=96",
"description": "",
"registered": "-001-11-30T00:00:00+00:00",
"meta": {
"links": {
"self": "url.com/wp-json/users/1",
"archives": "url.com/wp-json/users/1/posts"
}
}
},
"content": "<p>Join us if you are interested in joining </p>\n",
"parent": 0,
"link": "url.com/calendar/worship-service-practice/2014-12-06/",
"date": "2014-12-04T20:09:21-06:00",
"modified": "2014-12-04T20:09:21-06:00",
"format": "standard",
"slug": "worship-service-practice",
"guid": "url.com/?post_type=tribe_events&p=575",
"excerpt": "<p>Join us if you are interested i</p>\n",
"menu_order": 0,
"comment_status": "closed",
"ping_status": "closed",
"sticky": false,
"date_tz": "America/Chicago",
"date_gmt": "2014-12-05T02:09:21+00:00",
"modified_tz": "America/Chicago",
"modified_gmt": "2014-12-05T02:09:21+00:00",
"meta": {
"links": {
"self": "url.com/wp-json/posts/575",
"author": "url.com/wp-json/users/1",
"collection": "url.com/wp-json/posts",
"replies": "url.com/wp-json/posts/575/comments",
"version-history": "url.com/wp-json/posts/575/revisions"
}
},
"post_meta": {
"EventStartDate": "2014-12-06 08:00:00",
"EventEndDate": "2014-12-06 09:00:00"
},
"featured_image": null,
"terms": {
"tribe_events_cat": [
{
"ID": 13,
"name": "Community Outreach",
"slug": "community-outreach",
"description": "",
"parent": null,
"count": 52,
"link": "url.com/calendar/category/community-outreach/",
"meta": {
"links": {
"collection": "url.com/wp-json/taxonomies/tribe_events_cat/terms",
"self": "url.com/wp-json/taxonomies/tribe_events_cat/terms/9"
}
}
}
]
}
},
"link": "url.com/calendar/worship-service-practice/2014-12-13/",
"date": "2014-12-04T20:09:21-06:00",
"modified": "2014-12-04T20:09:21-06:00",
"format": "standard",
"slug": "worship-service-practice-2014-12-13",
"guid": "url.com/?post_type=tribe_events&eventDate=2014-12-13#038;p=575",
"excerpt": "<p>Join us if you are interested </p>\n",
"menu_order": 0,
"comment_status": "closed",
"ping_status": "closed",
"sticky": false,
"date_tz": "America/Chicago",
"date_gmt": "2014-12-05T02:09:21+00:00",
"modified_tz": "America/Chicago",
"modified_gmt": "2014-12-05T02:09:21+00:00",
"meta": {
"links": {
"self": "url.com/wp-json/posts/576",
"author": "url.com/wp-json/users/1",
"collection": "url.com/wp-json/posts",
"replies": "url.com/wp-json/posts/576/comments",
"version-history": "url.com/wp-json/posts/576/revisions",
"up": "url.com/wp-json/posts/575"
}
},
"post_meta": {
"EventStartDate": "2014-12-13 08:00:00",
"EventEndDate": "2014-12-13 09:00:00"
},
"featured_image": null,
"terms": {
"tribe_events_cat": [
{
"ID": 13,
"name": "Community Outreach",
"slug": "community-outreach",
"description": "",
"parent": null,
"count": 52,
"link": "url.com/calendar/category/community-outreach/",
"meta": {
"links": {
"collection": "url.com/wp-json/taxonomies/tribe_events_cat/terms",
"self": "url.com/wp-json/taxonomies/tribe_events_cat/terms/9"
}
}
}
]
}
},
{…
I can't for the life of me get the data from the Event class to display on the table. I have 10 empty rows display on the table with the disclosure indicator but no text. When I throw an NSLog into the cellForRowAtIndexPath function and attempt to print event.title it logs as null. Any ideas on what I'm doing wrong?

You never set event.title. Also the way you iterate through the json is incorrect. If it is a property you should set it like so:
for(NSDictionary *eventDic in [parsedObject objectForKey:#"results"]){
Event *event = [[Event alloc] init];
event.title = eventDic[#"title"]; //shorthand for [eventDic objectForKey:#"title"];
event.link = eventDic[#"link"];
[events addObject:event];
}

It turns out my code was fine, but when I setup my Event class I made the attributes weak vs strong. Once making the properties attributes strong, it retained all of the pointers just fine and everything was good to go.

Related

Maintain order of elements of NSMutableDictionary [duplicate]

This question already has answers here:
Maintaining the order while iterating a NSDictionary
(3 answers)
Closed 3 years ago.
I am getting a dictionary of the following format :
[
{
"position": "1",
"name": "Jaen",
"group": "Student",
"Address": "Delhi"
},
{
"position": "2",
"name": "Jaen",
"group": "Student",
"Address": "Delhi"
},
{
"position": "1",
"name": "Jaen",
"group": "Teacher",
"Address": "Delhi"
}
]
Basically, I want to group them around the key named "group".I have to create a dictionary in the following format to populate data in my UI.
[
{
"Student": [
{
"position": "1",
"name": "Jaen",
"group": "Student",
"Address": "Delhi"
},
{
"position": "2",
"name": "Jaen",
"group": "Student",
"Address": "Delhi"
}
],
"Teacher": [
{
"position": "1",
"name": "Jaen",
"group": "Teacher",
"Address": "Delhi"
}
]
}
]
the order of the Groups should be maintained ie Student should come before Teacher. But when I am printing dictGroupField it is giving random results in terms of order.
How can i maintain the order?
Please find the code below that I was using :
#property (strong, nonatomic) NSMutableDictionary *dictGroupField;
-(void)loadFieldData {
_dictGroupField = [[NSMutableDictionary alloc] init];
NSMutableArray *arrayFields = [self getArrayFields];
for (NSDictionary *dictT in arrayFields) {
NSString *strGroup = [dict valueForKey:#"group"];
if ([_dictGroupField valueForKey:strGroup] == nil) {
NSMutableArray *arrGroup = [[NSMutableArray alloc] init];
[arrGroup addObject:dict];
[_dictGroupField setObject:arrGroup forKey:strGroup];
} else {
NSMutableArray *arrGroupExist = [_dictGroupField valueForKey:strGroup];
[arrGroupExist addObject:dict];
[_dictGroupField setObject:arrGroupExist forKey:strGroup];
}
}
}
You can't maintain it order. Dictionaries are unordered collections of key-value associations. See more Docs

facebook graph api for poste is not show all data in ios

i am working one facebook intrgation project in ios,iphone
so i am using me?fields=posts on browser is working properly
i means on this link developer account
https://developers.facebook.com/tools/explorer/145634995501895/?method=GET&path=me%3Ffields%3Dposts
but when i am using me?fields=posts in xcode this only return a old data not all data
here is my code so please give me solution where i am doing mistake
[FBRequestConnection startWithGraphPath:#"me?fields=posts"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(#"Results: %#", result);
NSArray *post = [result valueForKey:#"posts"];
NSArray *mainData = [post valueForKey:#"data"];
/// NSLog(#"Results: show image>>>>>>> %#", [mainData valueForKey:#"picture"]);
//
NSArray *picturedata =[mainData valueForKey:#"picture"];
NSLog(#"%#",picturedata);
Here i attacha a image link also please check
image 1
in Graph api data is
{
"posts": {
"data": [
{
"id": "100005440749818_240650716126285",
"from": {
"name": "Birjesh Sharma",
"id": "100005440749818"
},
"message": "Hello\r\n\r\n\r\n",
"picture": "https://fbexternal-a.akamaihd.net/app_full_proxy.php?app=278318608948142&v=1&size=z&cksum=9c977855a08e5bde268444a1ab24a254&src=https%3A%2F%2Fraw.github.com%2Ffbsamples%2Fios-3.x-howtos%2Fmaster%2FImages%2Fiossdk_logo.png",
"link": "https://developers.facebook.com/ios",
"name": "Facebook SDK for iOS",
"caption": "Build great social apps and get more installs.",
"description": "The Facebook SDK for iOS makes it easier and faster to develop Facebook integrated iOS apps.",
"icon": "https://www.facebook.com/images/litestand/bookmarks/sidebar/icons/small/icon-app.png",
"actions": [
{
"name": "Comment",
"link": "https://www.facebook.com/100005440749818/posts/240650716126285"
},
{
"name": "Like",
"link": "https://www.facebook.com/100005440749818/posts/240650716126285"
}
],
"privacy": {
"description": "Friends",
"value": "ALL_FRIENDS",
"friends": "",
"networks": "",
"allow": "",
"deny": ""
},
"type": "link",
"status_type": "app_created_story",
"application": {
"name": "Feed Dialog How To",
"id": "278318608948142"
},
"created_time": "2014-04-26T12:59:15+0000",
"updated_time": "2014-04-26T12:59:15+0000"
},
{
"id": "100005440749818_240650712792952",
"from": {
"name": "Birjesh Sharma",
"id": "100005440749818"
},
"message": "Hello\r\n\r\n\r\n",
"picture": "https://fbexternal-a.akamaihd.net/app_full_proxy.php?app=278318608948142&v=1&size=z&cksum=9c977855a08e5bde268444a1ab24a254&src=https%3A%2F%2Fraw.github.com%2Ffbsamples%2Fios-3.x-howtos%2Fmaster%2FImages%2Fiossdk_logo.png",
"link": "https://developers.facebook.com/ios",
"name": "Facebook SDK for iOS",
"caption": "Build great social apps and get more installs.",
"description": "The Facebook SDK for iOS makes it easier and faster to develop Facebook integrated iOS apps.",
"icon": "https://www.facebook.com/images/litestand/bookmarks/sidebar/icons/small/icon-app.png",
"actions": [
{
"name": "Comment",
"link": "https://www.facebook.com/100005440749818/posts/240650712792952"
},
{
"name": "Like",
"link": "https://www.facebook.com/100005440749818/posts/240650712792952"
}
],
"privacy": {
"description": "Friends",
"value": "ALL_FRIENDS",
"friends": "",
"networks": "",
"allow": "",
"deny": ""
},
"type": "link",
"status_type": "app_created_story",
"application": {
"name": "Feed Dialog How To",
"id": "278318608948142"
},
"created_time": "2014-04-26T12:59:14+0000",
"updated_time": "2014-04-26T12:59:14+0000"
},
{
"id": "100005440749818_240643612793662",
"from": {
"name": "Birjesh Sharma",
"id": "100005440749818"
},
"message": "Hello how are you",
"actions": [
{
"name": "Comment",
"link": "https://www.facebook.com/100005440749818/posts/240643612793662"
},
{
"name": "Like",
"link": "https://www.facebook.com/100005440749818/posts/240643612793662"
}
],
"privacy": {
"description": "Friends",
"value": "ALL_FRIENDS",
"friends": "",
"networks": "",
"allow": "",
"deny": ""
},
"type": "status",
"status_type": "mobile_status_update",
"application": {
"name": "Batch Requests How To",
"id": "216458921816803"
},
"created_time": "2014-04-26T12:20:43+0000",
"updated_time": "2014-04-26T12:20:43+0000"
},
{
"id": "100005440749818_240368252821198",
"from": {
"name": "Birjesh Sharma",
"id": "100005440749818"
},
"story": "Birjesh Sharma added a new photo.",
"picture": "https://fbcdn-photos-e-a.akamaihd.net/hphotos-ak-prn2/t1.0-0/10155869_240368246154532_637168545169195821_s.jpg",
"link": "https://www.facebook.com/photo.php?fbid=240368246154532&set=a.170520606472630.1073741830.100005440749818&type=1&relevant_count=1",
"name": "HelloFBSample Photos",
"icon": "https://fbstatic-a.akamaihd.net/rsrc.php/v2/yz/r/StEh3RhPvjk.gif",
"actions": [
{
"name": "Comment",
"link": "https://www.facebook.com/100005440749818/posts/240368252821198"
},
{
"name": "Like",
"link": "https://www.facebook.com/100005440749818/posts/240368252821198"
}
],
"privacy": {
"description": "Only Me",
"value": "SELF",
"friends": "",
"networks": "",
"allow": "",
"deny": ""
},
"type": "photo",
"status_type": "added_photos",
"object_id": "240368246154532",
"application": {
"name": "HelloFBSample",
"namespace": "fbsdktemplateapp",
"id": "355198514515820"
},
"created_time": "2014-04-25T13:26:06+0000",
"updated_time": "2014-04-25T13:26:06+0000"
},
{
"id": "100005440749818_240333912824632",
"from": {
"name": "Birjesh Sharma",
"id": "100005440749818"
},
"message": "Post by nishant",
"picture": "https://fbcdn-photos-f-a.akamaihd.net/hphotos-ak-prn2/t1.0-0/10155899_240333846157972_3108893745639530212_s.jpg",
"link": "https://www.facebook.com/photo.php?fbid=240333846157972&set=a.239581289566561.1073741833.100005440749818&type=1&relevant_count=1",
"icon": "https://fbstatic-a.akamaihd.net/rsrc.php/v2/yz/r/StEh3RhPvjk.gif",
"actions": [
{
"name": "Comment",
"link": "https://www.facebook.com/100005440749818/posts/240333912824632"
},
{
"name": "Like",
"link": "https://www.facebook.com/100005440749818/posts/240333912824632"
}
],
"privacy": {
"description": "Public",
"value": "EVERYONE",
"friends": "",
"networks": "",
"allow": "",
"deny": ""
},
"type": "photo",
"status_type": "added_photos",
"object_id": "240333846157972",
"created_time": "2014-04-25T10:33:45+0000",
"updated_time": "2014-04-25T10:33:45+0000",
"likes": {
"data": [
{
"id": "100005440749818",
"name": "Birjesh Sharma"
}
],
"paging": {
"cursors": {
"after": "MTAwMDA1NDQwNzQ5ODE4",
"before": "MTAwMDA1NDQwNzQ5ODE4"
}
}
}
},
and when i am using this graph api in xcode the data show this
Results: {
id = 100005440749818;
posts = {
data = (
{
"created_time" = "2014-04-17T14:13:12+0000";
from = {
id = 100005440749818;
name = "Birjesh Sharma";
};
id = "100005440749818_238353183022705";
link = "https://www.facebook.com/nitin.dhiran.1?fref=nf_fr";
picture = "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn2/t1.0-1/c0.9.50.50/p50x50/10262125_697172320343788_6250762651875413687_t.jpg";
privacy = {
value = "";
};
"status_type" = "approved_friend";
story = "Birjesh Sharma is now friends with Nitin Dhiran and 3 other people.";
"story_tags" = {
0 = (
{
id = 100005440749818;
length = 14;
name = "Birjesh Sharma";
offset = 0;
type = user;
}
);
35 = (
{
id = 100001530793389;
length = 12;
name = "Nitin Dhiran";
offset = 35;
type = user;
}
);
52 = (
{
id = 100008058972523;
length = 14;
name = "Hemant Kumar";
offset = 52;
type = user;
},
{
id = 100001307401657;
length = 14;
name = "Chetan Pushpad";
offset = 52;
type = user;
},
{
id = 100002675972915;
length = 14;
name = "Vipin Shukla";
offset = 52;
type = user;
}
);
};
type = link;
"updated_time" = "2014-04-17T14:13:12+0000";
},
{
"created_time" = "2014-03-27T07:21:35+0000";
from = {
id = 100005440749818;
name = "Birjesh Sharma";
};
id = "100005440749818_232528183605205";
privacy = {
value = "";
};
"status_type" = "approved_friend";
story = "Birjesh Sharma is now friends with Ishant Tiwari and Viraj Dongre.";
"story_tags" = {
0 = (
{
id = 100005440749818;
length = 14;
name = "Birjesh Sharma";
offset = 0;
type = user;
}
);
35 = (
{
id = 100000329153640;
length = 13;
name = "Ishant Tiwari";
offset = 35;
type = user;
}
);
53 = (
{
id = 100002123975517;
length = 12;
name = "Viraj Dongre";
offset = 53;
type = user;
}
);
};
type = status;
"updated_time" = "2014-03-27T07:21:35+0000";
}
);
paging = {
next = "https://graph.facebook.com/100005440749818/posts?access_token=CAAUTJQ2n428BAN3p9kiFFMZA7ZBlDO2NTIddfkUNJdbqT32kfbVs8GJaORmJXTF4NnF6RUtIwwp0H0vTzS147lxFF3runTHbupqZBkxbyl73dV5URHwvZA78CqVXWJggaofAy2JfZBf1GCoUQfNCyN0TAJaEnnsLn9ea6d2wLtDAataFtxGyFQJwZBWajQOiIwtidXqliCFV8haorHgfE4ahdbpO5n7oUZD&limit=25&until=1395904894";
previous = "https://graph.facebook.com/100005440749818/posts?access_token=CAAUTJQ2n428BAN3p9kiFFMZA7ZBlDO2NTIddfkUNJdbqT32kfbVs8GJaORmJXTF4NnF6RUtIwwp0H0vTzS147lxFF3runTHbupqZBkxbyl73dV5URHwvZA78CqVXWJggaofAy2JfZBf1GCoUQfNCyN0TAJaEnnsLn9ea6d2wLtDAataFtxGyFQJwZBWajQOiIwtidXqliCFV8haorHgfE4ahdbpO5n7oUZD&limit=25&since=1397743992&__previous=1";
};
};
}
The posts list is divided into several pages, and you only got the first page. To get all the posts, use the URL in results.posts.paging.next.
You can use this FBSDKGraphRequest paging extension to do that for you.

Parsing SoundCloud - Change API Call, not receiving JSON data?

I'm playing around with the SoundCloud iOS tutorial and have been changing the API calls to get different information.
However, I can't seem to get any JSON back for a call I make to the API when I try to do a GET request on the URL https://api.soundcloud.com/me/activities/tracks/affiliated.json. The GET request works in the API console. Other GET requests work too, so I'm thinking that it's the definitely the way I'm asking for information in Xcode.
Here's the "Money" part of the program - gets the data, parses, creates a new view controller that has an array as a property with the response.
SCRequestResponseHandler handler;
handler = ^(NSURLResponse *response, NSData *data, NSError *error) {
NSError *jsonError = nil;
NSJSONSerialization *jsonResponse = [NSJSONSerialization
JSONObjectWithData:data
options:0
error:&jsonError];
if (!jsonError && [jsonResponse isKindOfClass:[NSArray class]]) {
UIStoryboard *sb = self.storyboard;
SCTTrackListViewController *trackListVC;
trackListVC = [sb instantiateViewControllerWithIdentifier:#"TLVC"];
trackListVC.tracks = (NSArray *)jsonResponse;
// Am I getting JSON back for tracks/affiliated.json ?
NSLog(#"json %#",(NSArray *)jsonResponse);
[self presentViewController:trackListVC
animated:YES
completion:nil];
}
};
NSString *resourceURL = #"https://api.soundcloud.com/me/activities/tracks/affiliated.json";
[SCRequest performMethod:SCRequestMethodGET
onResource:[NSURL URLWithString:resourceURL]
usingParameters:nil
withAccount:account
sendingProgressHandler:nil
responseHandler:handler];
}
If I use NSString *resourceURL = #"https://api.soundcloud.com/me/tracks.json"; or NSString *resourceURL = #"https://api.soundcloud.com/me/favorites.json"; then I get a list of tracks returned in JSON. If I use the one in the code example, I get no response - total silence.
The JSON response I get for https://api.soundcloud.com/me/favorites.json is in the form of
[
{
"kind": "track",
"id": 112547469,
"created_at": "2013/09/26 06:50:06 +0000",
"user_id": 294371,
"duration": 4499751,
"commentable": true,
"state": "finished",
"original_content_size": 143977742,
"sharing": "public",
"tag_list": "Funk Disco",
"permalink": "the-boogie-down",
"streamable": true,
"embeddable_by": "all",
"downloadable": false,
"purchase_url": null,
"label_id": null,
"purchase_title": null,
"genre": "Boogie",
"title": "The Boogie Down",
"description": "1.\tWar - The World Is A Ghetto (Suonho Edit)\r\n\r\n2. \tWhiskey Barons - The Same Love\r\n\r\n3.\tRayko - Broadway\r\n\r\n4.\tNicholas - Talking About Love\r\n\r\n5.\tBen E. King - Supernatural Thing (Fingerman Edit)\r\n\r\n6.\tGazeebo - Scaredy Cat\r\n\r\n7.\tFingerman - Fat Like You Know\r\n\r\n8.\tWillie Beaver - Party Time (Karim Edit)\r\n\r\n9.\tDeadly Sins - I Can Feel It\r\n\r\n10.\tFingerman - Tootin\r\n\r\n11.\tRocco Raimundo - Give Me Your Love\r\n\r\n12.\tLuther Vandross - Never Too Much\r\n\r\n13.\tKool & The Gang - Get Down On It (Rocco Raimundo Edit)",
"label_name": "",
"release": "",
"track_type": "",
"key_signature": "",
"isrc": "",
"video_url": null,
"bpm": null,
"release_year": null,
"release_month": null,
"release_day": null,
"original_format": "mp3",
"license": "all-rights-reserved",
"uri": "https://api.soundcloud.com/tracks/112547469",
"user": {
"id": 294371,
"kind": "user",
"permalink": "web_d",
"username": "webd",
"uri": "https://api.soundcloud.com/users/294371",
"permalink_url": "http://soundcloud.com/web_d",
"avatar_url": "https://i1.sndcdn.com/avatars-000011907556-1k1cgw-large.jpg?3eddc42"
},
"user_playback_count": 1,
"user_favorite": true,
"permalink_url": "http://soundcloud.com/web_d/the-boogie-down",
"artwork_url": "https://i1.sndcdn.com/artworks-000058671332-jzz9uc-large.jpg?3eddc42",
"waveform_url": "https://w1.sndcdn.com/qA5zJCmDqO46_m.png",
"stream_url": "https://api.soundcloud.com/tracks/112547469/stream",
"playback_count": 69,
"download_count": 0,
"favoritings_count": 1,
"comment_count": 0,
"attachments_uri": "https://api.soundcloud.com/tracks/112547469/attachments"
},
whilst the response for https://api.soundcloud.com/me/activities/tracks/affiliated.json goes:
{
"collection": [
{
"type": "track",
"created_at": "2013/10/08 20:14:16 +0000",
"origin": {
"kind": "track",
"id": 114421587,
"created_at": "2013/10/08 18:20:21 +0000",
"user_id": 144598,
"duration": 340616,
"commentable": true,
"state": "finished",
"original_content_size": 13624479,
"sharing": "public",
"tag_list": "",
"permalink": "faden-away",
"streamable": true,
"embeddable_by": "all",
"downloadable": false,
"purchase_url": null,
"label_id": null,
"purchase_title": null,
"genre": "Funk",
"title": "Faden Away",
"description": "",
"label_name": "",
"release": "",
"track_type": "",
"key_signature": "",
"isrc": "",
"video_url": null,
"bpm": null,
"release_year": null,
"release_month": null,
"release_day": null,
"original_format": "mp3",
"license": "all-rights-reserved",
"uri": "https://api.soundcloud.com/tracks/114421587",
"user": {
"id": 144598,
"kind": "user",
"permalink": "stonesthrow",
"username": "Stones Throw Records",
"uri": "https://api.soundcloud.com/users/144598",
"permalink_url": "http://soundcloud.com/stonesthrow",
"avatar_url": "https://i1.sndcdn.com/avatars-000001771161-2x04bn-large.jpg?3eddc42"
},
"user_playback_count": 1,
"user_favorite": false,
"permalink_url": "http://soundcloud.com/stonesthrow/faden-away",
"artwork_url": "https://i1.sndcdn.com/artworks-000059640743-qtexag-large.jpg?3eddc42",
"waveform_url": "https://w1.sndcdn.com/NBZ15f1BZ4cV_m.png",
"stream_url": "https://api.soundcloud.com/tracks/114421587/stream",
"playback_count": 29756,
"download_count": 0,
"favoritings_count": 1587,
"comment_count": 138,
"attachments_uri": "https://api.soundcloud.com/tracks/114421587/attachments",
"sharing_note": {
"text": "",
"created_at": "2013/10/08 20:14:16 +0000"
}
},
"tags": "affiliated"
},
{
"type": "track",
"created_at": "2013/10/08 14:26:00 +0000",
"origin": {
"kind": "track",
"id": 112820793,
"created_at": "2013/09/27 21:39:16 +0000",
"user_id": 1520490,
"duration": 290926,
"commentable": true,
"state": "finished",
"original_content_size": 12579855,
"sharing": "public",
"tag_list": ""MUSEUM OF LOVE" "DFA RECORDS" "PAT MAHONEY" "LCD SOUNDSYSTEM" "THE JUAN MACLEAN" JEE DAY DFA",
"permalink": "museum-of-love-monotronic",
"streamable": true,
"embeddable_by": "all",
"downloadable": false,
"purchase_url": "https://itunes.apple.com/album/monotronic-single/id711563768",
"label_id": null,
"purchase_title": "DIGITAL SINGLE",
"genre": "",
"title": "Museum of Love - Monotronic",
"description": "Museum Of Love\n"Monotronic"\n\nDFA2390\n\nfirst single "Down South" available here: http://store.dfarecords.com/products/dfa2389",
"label_name": "DFA Records",
"release": "DFA2390",
"track_type": "original",
"key_signature": "",
"isrc": "",
"video_url": "http://www.youtube.com/watch?v=K2E6oK7tN5w",
"bpm": null,
"release_year": 2013,
"release_month": 10,
"release_day": 8,
"original_format": "mp3",
"license": "all-rights-reserved",
"uri": "https://api.soundcloud.com/tracks/112820793",
"user": {
"id": 1520490,
"kind": "user",
"permalink": "dfa-records",
"username": "DFA Records",
"uri": "https://api.soundcloud.com/users/1520490",
"permalink_url": "http://soundcloud.com/dfa-records",
"avatar_url": "https://i1.sndcdn.com/avatars-000002067008-el39h6-large.jpg?3eddc42"
},
"user_playback_count": 1,
"user_favorite": false,
"permalink_url": "http://soundcloud.com/dfa-records/museum-of-love-monotronic",
"artwork_url": "https://i1.sndcdn.com/artworks-000058798982-ogzv3j-large.jpg?3eddc42",
"waveform_url": "https://w1.sndcdn.com/0m0pwUhjvenc_m.png",
"stream_url": "https://api.soundcloud.com/tracks/112820793/stream",
"playback_count": 10047,
"download_count": 0,
"favoritings_count": 309,
"comment_count": 18,
"attachments_uri": "https://api.soundcloud.com/tracks/112820793/attachments",
"sharing_note": {
"text": "",
"created_at": "2013/09/27 21:39:16 +0000"
}
},
"tags": "affiliated"
},
I'm trying to store the response in an NSArray, and I think that might be the issue... can someone point me in the right direction to get the JSON for affiliated.json? Is the response of the second JSON a dictionary? Do I need to store the response for that one differently? I thought I could store a dictionary in an NSArray...
The reason I was getting back nothing was due to this line: NSLog(#"json %#",(NSArray *)jsonResponse); where I was casting jsonResponse as an NSArray. This worked in the first instance because the response was an array, but in the second instance the response was a dictionary. Pretty obvious now that I think about it. I changed that line to (NSDictionary *)jsonResponse and all was well.
After I received the JSON data, this was also a good way to experiment with the different ways to dig into the nested info in the data. For instance, I created a dictionary to hold jsonResponse. I could get the info for the key I wanted in a couple of ways:
dictionary[#"user"][#"avatar_url] would get the nested value for avatar_url which was itself nested in user.
I could also do this by calling [dictionary valueForKeyPath:#"user.avatar_url"];

Parsing Json to get all the contents in one NSArray

That's the content...
[
{
"id": "",
"title": "",
"website": "",
"categories": [
{
"id": "",
"label": ""
}
],
"updated":
},
{
"id": "",
"title": "",
"website": "",
"categories": [
{
"id": "",
"label": ""
}
],
"updated":
}
]
How can I insert every feed source in one array?
NSDictionary *results = [string JSONValue];
NSArray *subs = [results valueForKey:#"KEY"];
Which key I must insert?
THanks
as I can see your structure, you will get out of this JSON-String
NSArray:
[
NSDictionary:
{
NSString: "id",
NSString: "title",
NSString: "website",
NSArray: "categories":
[
NSDictionary:
{
NSString: "id",
NSString: "label"
}
],
NSNumber: "updated"
},
NSDictionary:
{
...
}
]
So you have already an array of "Feeds" at root and you have to itterate them with their index in the array with. For first id i.e. [[myJsonStructure objectAtIndex:0] objectForKey:#"id"];

Parsing JSON in Facebook SDK 3.0 iOS

I'm trying to figure out how to parse some Facebook JSON data I get back using graphPath:#"me/home".
Im assuming the data returned from
FBRequest *request = [[FBRequest alloc] initWithSession:FBSession.activeSession
graphPath:#"me/home"];
doesn't need parsing, and i just need to extract the data through dictionary keys, im finding this pretty confusing. Im trying to put the data into a tableview, basically showing the users news feed from Facebook. Heres a snippet of data returned:
"data": [
{
"id": "1156410856_4453627016238",
"from": {
"name": "Abbi ☆ Mathews",
"id": "1156410856"
},
"message": "Aalllllllll over this tonight.......be there....gonna be booooom ting!",
"picture": "http://photos-a.ak.fbcdn.net/hphotos-ak-snc6/185169_4453626736231_812066502_s.jpg",
"link": "http://www.facebook.com/photo.php?fbid=4453626736231&set=a.1166926850788.88722.1156410856&type=1&relevant_count=1",
"icon": "http://static.ak.fbcdn.net/rsrc.php/v2/yx/r/og8V99JVf8G.gif",
"actions": [
{
"name": "Comment",
"link": "http://www.facebook.com/1156410856/posts/4453627016238"
},
{
"name": "Like",
"link": "http://www.facebook.com/1156410856/posts/4453627016238"
}
],
"privacy": {
"description": "Friends; Except: Restricted",
"value": "ALL_FRIENDS"
},
"type": "photo",
"object_id": "4453626736231",
"application": {
"name": "Facebook for Android",
"namespace": "fbandroid",
"id": "350685531728"
},
"created_time": "2012-08-25T14:08:11+0000",
"updated_time": "2012-08-25T14:08:11+0000",
"comments": {
"count": 0
}
},
{
"id": "7147617470_10150998196922471",
"from": {
"name": "The Fratellis",
"category": "Musician/band",
"id": "7147617470"
},
"picture": "http://platform.ak.fbcdn.net/www/app_full_proxy.php?app=123966167614127&v=1&size=z&cksum=77469f27ebc292a58b3ee8ffb8183582&src=https%3A%2F%2Fd12nfv3nknyhzq.cloudfront.net%2Fimages%2Fartists%2F1208090326852218_medium.jpg",
"link": "http://www.bandsintown.com/event/5469167/facebook_rsvp?artist=The+Fratellis&came_from=90",
"name": "Next Month: The Fratellis # Academy in Oxford, United Kingdom",
"caption": "Saturday, September 22, 2012 at 7:00pm",
"properties": [
{
"name": "Tickets",
"text": "http://bnds.in/Nq9prK",
"href": "http://www.bandsintown.com/event/5469167/buy_tickets?affil_code=fb_7147617470_auto_promote_month_before_targeted&artist=The+Fratellis"
},
{
"name": "More Tour Dates",
"text": "http://bnds.in/O9NLwd",
"href": "http://www.bandsintown.com/TheFratellis/facebookapp?came_from=90"
}
],
"icon": "http://photos-b.ak.fbcdn.net/photos-ak-snc7/v85006/251/123966167614127/app_2_123966167614127_7663.gif",
"actions": [
{
"name": "Comment",
"link": "http://www.facebook.com/7147617470/posts/10150998196922471"
},
{
"name": "Like",
"link": "http://www.facebook.com/7147617470/posts/10150998196922471"
},
{
"name": "RSVP",
"link": "http://www.bandsintown.com/event/5469167/facebook_rsvp?artist=The+Fratellis&came_from=90"
}
],
"privacy": {
"description": "United Kingdom",
"value": "CUSTOM"
},
"type": "link",
"application": {
"name": "Bandsintown",
"namespace": "concertsbybit",
"id": "123966167614127"
},
"created_time": "2012-08-25T14:06:39+0000",
"updated_time": "2012-08-25T14:14:01+0000",
"likes": {
"data": [
{
"name": "Derek Moore",
"id": "536043117"
},
{
"name": "Louise Berry",
"id": "1310689257"
},
{
"name": "Jack Barber",
"id": "100002857426027"
},
{
"name": "Becky Hoddinott",
"id": "100000168277953"
}
],
"count": 8
},
"comments": {
"data": [
{
"id": "7147617470_10150998196922471_22699715",
"from": {
"name": "Becky Hoddinott",
"id": "100000168277953"
},
"message": "Yesssssss! <3",
"created_time": "2012-08-25T14:14:01+0000"
}
],
"count": 1
}
},
{
Ive been trying to put this into a tableview, but the UILabels are just blank, so what keys do i use to get this to work??
Heres All the Code:
-(void)sendRequests {
FBRequestConnection *connection = [[FBRequestConnection alloc] init];
FBRequest *request = [[FBRequest alloc] initWithSession:FBSession.activeSession
graphPath:#"me/home"];
[connection addRequest:request completionHandler:
^(FBRequestConnection *connection, id result, NSError *error) {
if (!error && result) {
NSLog(#"Pre Parsed Data = %#", result);
NSArray *arrayRsult = [result objectForKey:#"data"];
for (_FBData in arrayRsult){
[self.tableView reloadData];
}
} else if (error) {
NSLog(#"Error Fetching Data = %#", error);
}
}];
[connection start];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
FaceBookCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[FaceBookCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
NSLog(#"Intialise Cell");
}
NSMutableDictionary *fbText = [_FBData objectAtIndex:indexPath.row];
NSString *name = [[fbText objectForKey:#"from"] objectForKey:#"name"];
NSString *dateCreated = [fbText objectForKey:#"created_time"];
NSString *message = [fbText objectForKey:#"message"];
[cell.name setText:name];
[cell.message setText:message];
[cell.time setText:dateCreated];
NSLog(#"Names = %#", name);
return cell;
}

Resources