How To Fetch and Parse JSON Using iOS SDK

176 Flares 176 Flares ×

Editor’s Note: This week, Ziad Tamim will show you how to fetch and parse JSON. In this tutorial, we will be focusing on adding JSON support to your iOS apps by demonstrating how to create a simple app using Meetup API. The tutorial may be a bit complicated than other tutorials we covered before. You’re expected to have basic knowledge of JSON and design patterns.

Enter the JSON programming tutorial.

First, what’s JSON? JSON (short for JavaScript Object Notation) is a text-based, lightweight and easy way for storing and exchanging data. It’s commonly used for representing structural data and data interchange in client-server applications, serving as an alternative to XML. A lot of the services we use everyday have JSON-based APIs. Most of the iOS apps including Twitter, Facebook and Flick send data to their backend web services in JSON format.

json intro tutorial

As an example, a JSON representation of a Movie object may look like this

{
    "title": "The Amazing Spider-man",
    "release_date": "03/07/2012",
    "director": "Marc Webb",
    "cast": [
        {
            "name": "Andrew Garfield",
            "character": "Peter Parker"
        },
        {
            "name": "Emma Stone",
            "character": "Gwen Stacy"
        },
        {
            "name": "Rhys Ifans",
            "character": "Dr. Curt Connors"
        }
    ]
}

As you can see, the JSON data is human-readable and easier to parse than XML. If you still have no idea about JSON, check out JSON Guide to learn more about it.

Since the release of iOS 5, the iOS SDK makes it easy to fetch and parse JSON data. In this tutorial, we’ll demonstrate how to use the built-in API to query a JSON-based API from Meetup and deal with the returned data appropriately.

Take a Look at Our App

Before we move onto the code, let’s take a look at what we’ll build.

In this tutorial, we’ll create a simple app called BrowseMeetup that consumes Meetup’s public API. If you haven’t heard of Meetup, it is the world’s largest network of local groups. You’re free to use Meetup to organize a local group or find one of the thousands already meeting up face-to-face. Like other social networks, it provides open API for accessing its data from your own apps.

The BrowseMeetup app will consume the web service of Meetup to look up for groups nearby. The app will get the current location and then automatically load the nearby Meetup groups.

BrowseMeetup Demo App

Note: This app utilizes Core Location framework. If you don’t have any idea about it, check out our tutorial about How To Get the User Location in iPhone App.

Getting Started

It’s time to set up the project to manage the BrowseMeetup application. Launch Xcode and create a new project based on the Master-Detail Application iOS app template. In the project options, you’ll need to check the Storyboard and Automatic Reference Counting boxes. This app will provide only iPhone Views, so choose iPhone for the device family and save the project. Delete the DetailsViewController and in the storyboard, design the user interface similar to the one below:

BrowseMeetup Storyboard

Storyboards of BrowseMeetup App

The focus of this tutorial is on JSON fetching and parsing. So to save your time from setting up the project, you can download this project template to start with. The template already pre-built the user interface and implemented the Core Location for you.

Tip: If you have no idea about table view and wonder how the navigation bar works, check out our tutorials in the free iOS course.

Working with Meetup APIs

Before you can use Meetup APIs, first create a new account on Meetup. Start by creating a new account by going to APIs Doc and clicking on the button “Request to join this Meetup group”, then fill the necessary information and hit “Sign Up”, then go step by step until its done.

Screen Shot 2013-08-26 at 10.06.27 PM

We’ll use one of the Meetup APIs (i.e. https://api.meetup.com/2/groups) for fetching the Meetup groups held at a certain location. The call allows developers to specify the location by using latitude and longitude. You can test the call by using the API console.

Here is a sample JSON response for this request (https://api.meetup.com/2/groups?&sign=true&lat=51.509980&lon=-0.133700&page=1):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
{
"results": [
{
"lon": -0.10000000149011612,
"visibility": "public",
"organizer": {
"name": "William Brown",
"member_id": 3817216
},
"link": "http://www.meetup.com/french-32/",
"state": "17",
"join_mode": "approval",
"who": "LFM members",
"country": "GB",
"city": "London",
"id": 63974,
"category": {
"id": 16,
"name": "language/ethnic identity",
"shortname": "language"
},
"topics": [
{
"id": 185,
"urlkey": "french",
"name": "French Language"
},
{
"id": 264,
"urlkey": "friends",
"name": "Friends"
},
{
"id": 3304,
"urlkey": "expatfrench",
"name": "Expat French"
}
],
"timezone": "Europe/London",
"group_photo": {
"photo_link": "http://photos3.meetupstatic.com/photos/event/7/4/a/b/600_929867.jpeg",
"highres_link": "http://photos3.meetupstatic.com/photos/event/7/4/a/b/highres_929867.jpeg",
"thumb_link": "http://photos3.meetupstatic.com/photos/event/7/4/a/b/thumb_929867.jpeg",
"photo_id": 929867
},
"created": 1034097734000,
"description": "<p>The London French Meetup is the biggest group of French speakers of all ages and nationalities in London. We hold regular events to meet up, talk in French and share interests in French culture whilst having a good time.</p>
<p>We have two main events per month where we have the whole of the upstairs of a pub.</p>
<p>In addition, we organise other regular events such as outings to: restaurants, trendy bars, french films, live music, sports related activities, outdoor events and more...</p>
<p>The organising team is made of volunteers from different nationalities and ages. Our members are made up of: 1/3 French nationals, 1/3 British nationals and 1/3 other nationalities and francophone countries. If you have any ideas or suggestions for events or would like to help please let us know.</p>
<p>A bientôt.</p>
<p>LFM Team.</p>",
"name": "London French Meetup",
"rating": 4.37,
"urlname": "french-32",
"lat": 51.52000045776367,
"members": 4889
}
],
"meta": {
"lon": -0.1337,
"count": 1,
"signed_url": "http://api.meetup.com/2/groups?radius=25.0&order=id&desc=false&offset=0&format=json&lat=51.50998&page=1&fields=&lon=-0.1337&sig_id=109020062&sig=4532ed8f987f940748ebfba0f483a26f756dcba3",
"link": "http://www.meetup.com/2/groups",
"next": "http://www.meetup.com/2/groups?radius=25.0&order=id&format=json&lat=51.50998&page=1&desc=false&offset=1&fields=&sign=true&lon=-0.1337",
"total_count": 4501,
"url": "http://www.meetup.com/2/groups?radius=25.0&order=id&format=json&lat=51.50998&page=1&desc=false&offset=0&fields=&sign=true&lon=-0.1337",
"id": "",
"title": "Meetup Groups v2",
"updated": 1377876449000,
"description": """",
"method": "Groups",
"lat": 51.50998
}
}

Application Design and How It Works

As mentioned, Meetup API provides a method for requesting groups in a certain location. The response data will be sent in JSON format. We’ll need an object that can retrieve the data and construct our domain object from the encoded data. Below is an overview of the application design showing how the classes are structured and how they work together to get the meetup groups:

BrowseMeetup - Design Overview

BrowseMeetup – Design Overview

It may be a bit complicated for some of you. But let me give you a brief walkthrough. We create a MeetupManager, in which it will request for Meetup groups for a particular location. Here the MeetupManager acts as a facade. If you haven’t heard of the Facade design pattern, you can think of it as a coordinator of other classes. The Facade tries to provide a simplified interface for the view controller and shields it from the underlying implementation.

The MeetupCommunicator class is used for communicating with the Meetup API. Once Meetup responds with the JSON-formatted response, we’ll pass it to GroupBuilder that constructs the Group object.

The MasterViewController uses Core Location to figure out the current location and informs the MeetupManager to get the Meetup groups for that location. The MeetupManager coordinates with other classes to retrieve the groups. Once the groups are retrieved, it communicates with the MasterViewController via the delegate and pass the groups found. The MasterViewController then presents the groups in the table view.

Creating JSON Data Model

We are going to start by implementing the model layer. The Group class represents the group information in the BrowseMeetup app and is used to store the group information returned by Meetup. Here is a sample JSON response of a Group object:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
{
     lon: -71.12999725341797,
     visibility: "public",
     organizer: {
          name: "Emma",
          member_id: 2161382
     },
     link: "http://www.meetup.com/bloggers/",
     state: "MA",
     join_mode: "closed",
     who: "Bloggers",
     country: "US",
     city: "Cambridge",
     id: 21458,
    category: {
          id: 34,
          name: "tech",
          shortname: "tech"
     },
     topics: [
          {
               id: 198,
               urlkey: "blog",
               name: "Blog"
          },
          {
               id: 772,
               urlkey: "writers",
               name: "Writers"
          }
     ],
     timezone: "US/Eastern",
     group_photo: {
     photo_link: "http://photos3.meetupstatic.com/photos/event/6/4/9/5/600_25749.jpeg",
     highres_link: "http://photos3.meetupstatic.com/photos/event/6/4/9/5/highres_25749.jpeg",
     thumb_link: "http://photos3.meetupstatic.com/photos/event/6/4/9/5/thumb_25749.jpeg",
     photo_id: 25749
     },
     created: 1034097731000,
     description: "This is a group for people that are interested in blogging and meeting others who are interested in blogging. Topics discussed range from blog content to blog software. All interest levels are welcome.",
     name: "The Greater Boston Area Weblogger Meetup Group",
     rating: 3.33,
     urlname: "bloggers",
     lat: 42.38999938964844,
     members: 119
}

The above JSON response represents a single Meetup Group. We’ll not use all the group data returned. But simply use the “name”, “description”, “who”, “country” and “city” fields. These fields are just good enough for presenting in the table view of our app. Now create a new file using the Objective-C class template. Name it Group, make it a subclass of NSObject and add the following code in the header file:

1
2
3
4
5
6
7
@interface Group : NSObject
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSString *description;
@property (strong, nonatomic) NSString *who;
@property (strong, nonatomic) NSString *country;
@property (strong, nonatomic) NSString *city;
@end

These properties are the information that we will use in our app to achieve the final result depicted earlier.

Fetching JSON Data Using Meetup API

First create a new file using Objective-C protocol template and name it as MeetupCommunicatorDelegate. Fill it with the below code:

1
2
3
4
@protocol MeetupCommunicatorDelegate
- (void)receivedGroupsJSON:(NSData *)objectNotation;
- (void)fetchingGroupsFailedWithError:(NSError *)error;
@end

The MeetupCommunicator class is responsible for the communication with the Meetup APIs and fetching the JSON data. It relies on the delegate of MeetupCommunicatorDelegate to handle the parsing of JSON data. The communicator has no idea how the JSON data is handled. Its focus is only on creating connection to the Meetup APIs and fetching the raw JSON result.

With the delegate created, create another class file and name it as MeetupCommunicator. Open the header file and put in the below code:

1
2
3
4
5
6
7
8
9
#import <CoreLocation/CoreLocation.h>

@protocol MeetupCommunicatorDelegate;

@interface MeetupCommunicator : NSObject
@property (weak, nonatomic) id<MeetupCommunicatorDelegate> delegate;

- (void)searchGroupsAtCoordinate:(CLLocationCoordinate2D)coordinate;
@end

We are creating a property to keep track of the communicator delegate, then defining a method for searching Meetup groups in a certain location. Next, open the MeetupCommunicator.m and place the code below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#import "MeetupCommunicator.h"
#import "MeetupCommunicatorDelegate.h"

#define API_KEY @"1f5718c16a7fb3a5452f45193232"
#define PAGE_COUNT 20

@implementation MeetupCommunicator

- (void)searchGroupsAtCoordinate:(CLLocationCoordinate2D)coordinate
{
    NSString *urlAsString = [NSString stringWithFormat:@"https://api.meetup.com/2/groups?lat=%f&lon=%f&page=%d&key=%@", coordinate.latitude, coordinate.longitude, PAGE_COUNT, API_KEY];
    NSURL *url = [[NSURL alloc] initWithString:urlAsString];
    NSLog(@"%@", urlAsString);
   
    [NSURLConnection sendAsynchronousRequest:[[NSURLRequest alloc] initWithURL:url] queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
       
        if (error) {
            [self.delegate fetchingGroupsFailedWithError:error];
        } else {
            [self.delegate receivedGroupsJSON:data];
        }
    }];
}

@end

The Meetup API needs a key to work. If you’re registered an account, you need to get one by going to API Key page. Simply click on the lock icon beside the textfield to reveal the key. Copy the text inside it and replace the value of the macro API_KEY with your own API key.

As mentioned in the earlier section, we use the following Meetup API for searching group in a specific location. The API accepts the location in the form of latitude and longitude. Here is a sample Meetup URL to connect:

1
https://api.meetup.com/2/groups?lat=51.509980&lon=-0.133700&page=20&key=1f5718c16a7fb3a5452f45193232

In the implementation of the method, we first construct the Meetup API URL with the specified latitude, longitude, the number of groups and the API key. In order not to blocking the UI, we load the data for a URL request asynchronously by using the “sendAsynchronousRequest:” method of NSURLConnection. Finally, when the JSON data is retrieved, it passes the data to the delegate for further processing.

Parsing JSON Data and Building Group Objects

When MeetupManager receives the data in JSON format, we use the class method of GroupBuilder to convert the data into Group objects. Create a new file with Objective-C class template and name it as GroupBuilder. Open the header file and paste the following code:

1
2
3
4
5
6
7
#import <Foundation/Foundation.h>

@interface GroupBuilder : NSObject

+ (NSArray *)groupsFromJSON:(NSData *)objectNotation error:(NSError **)error;

@end

Next, open “GroupBuilder.m” and implement the method with the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#import "GroupBuilder.h"
#import "Group.h"

@implementation GroupBuilder
+ (NSArray *)groupsFromJSON:(NSData *)objectNotation error:(NSError **)error
{
    NSError *localError = nil;
    NSDictionary *parsedObject = [NSJSONSerialization JSONObjectWithData:objectNotation options:0 error:&localError];
   
    if (localError != nil) {
        *error = localError;
        return nil;
    }
   
    NSMutableArray *groups = [[NSMutableArray alloc] init];
   
    NSArray *results = [parsedObject valueForKey:@"results"];
    NSLog(@"Count %d", results.count);
   
    for (NSDictionary *groupDic in results) {
        Group *group = [[Group alloc] init];
       
        for (NSString *key in groupDic) {
            if ([group respondsToSelector:NSSelectorFromString(key)]) {
                [group setValue:[groupDic valueForKey:key] forKey:key];
            }
        }
       
        [groups addObject:group];
    }
   
    return groups;
}
@end

The “groupsFromJSON:” method is designed to convert raw JSON data into an array of Group objects. Since the release of iOS 5, the iOS SDK comes with a class called NSJSONSerialization for parsing JSON data. Developers can use the class to convert JSON to Foundation objects or convert Foundation objects back to JSON.

When reading JSON data using NSJSONSerialization, all the keyed lists are automatically turned into NSDictionary objects. For array, it’s converted into NSArray instances. Any strings encountered along with the names of named items in keyed lists are converted into NSString, while purely numeric strings are converted into NSNumber objects. Lastly, any values of null are represented using NSNull.

Referring the sample response we showed you in earlier section, the Meetup API returns a JSON response with two main parts – results and meta. We’ll just need the “results” part. The code is very straightforward. We loop through the results and look into each NSDictionary inside it. We then create a Group object and fill it with the necessary information, then adding it to the mutable array.

Putting the Pieces Together with MeetupManager

Now you should know how to work with JSON, parse the data and convert it into objects. Next, we’ll implement the MeetupManager which serves as a coordinator of the underlying classes.

First, create a new file with the Objective-C protocol template and name it as MeetupManagerDelegate. Add the code below in the MeetupManagerDelegate.h:

1
2
3
4
@protocol MeetupManagerDelegate
- (void)didReceiveGroups:(NSArray *)groups;
- (void)fetchingGroupsFailedWithError:(NSError *)error;
@end

This delegate declares two methods and will be called by MeetupManager when the groups become available. The first method is called when the list of groups retrieved from Meetup is parsed, while the second method is invoked when an error occurred. The MeetupManagerDelegate will be implemented by MasterViewController that we’ll discuss in later section.

Next, create a new file with the Objective-C class template and name it MeetupManager. Then open the header file and add the code below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>

#import "MeetupManagerDelegate.h"
#import "MeetupCommunicatorDelegate.h"

@class MeetupCommunicator;

@interface MeetupManager : NSObject<MeetupCommunicatorDelegate>
@property (strong, nonatomic) MeetupCommunicator *communicator;
@property (weak, nonatomic) id<MeetupManagerDelegate> delegate;

- (void)fetchGroupsAtCoordinate:(CLLocationCoordinate2D)coordinate;
@end

As said, the MeetupManager acts as a façade. The app’s controller can work with the model object (i.e. Group) created without knowing any details about the network connection, JSON fetching/parsing or how the group objects are created. What the controller just needs to know is to use the “fetchGroupsAtCoordinate:” method for fetching Meetup groups.

We set up a property to keep an instance of our communicator which we will cover later and the other to keep track of the MeetupManagerDelegate. The “fetchGroupsAtCoordinate:” method will be used by the controller to fetch groups.

Next, open MeetupManager.m and place this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#import "GroupBuilder.h"
#import "MeetupCommunicator.h"

@implementation MeetupManager
- (void)fetchGroupsAtCoordinate:(CLLocationCoordinate2D)coordinate
{
    [self.communicator searchGroupsAtCoordinate:coordinate];
}

#pragma mark - MeetupCommunicatorDelegate

- (void)receivedGroupsJSON:(NSData *)objectNotation
{
    NSError *error = nil;
    NSArray *groups = [GroupBuilder groupsFromJSON:objectNotation error:&error];
   
    if (error != nil) {
        [self.delegate fetchingGroupsFailedWithError:error];
       
    } else {
        [self.delegate didReceiveGroups:groups];
    }
}

- (void)fetchingGroupsFailedWithError:(NSError *)error
{
    [self.delegate fetchingGroupsFailedWithError:error];
}

Here we implement the method fetchGroupsAtCoordinate:coordinate to fetch the groups in a certain area using the searchGroupsAtCoordinate:coordinate method of the communicator. We also implement the methods of MeetupCommunicatorDelegate for handling the JSON-formatted results as retrieved by the communicator.

The code inside the first method of the protocol receivedGroupsJSON:objectNotation uses the class method of GroupBuilder to convert the JSON result into Group objects and then informs its delegate with the Group objects. If there is any problem while processing the request, we invoke the other method (i.e. fetchingGroupsFailedWithError:) of the delegate to notify the controller that a problem is occurred.

Displaying the List of Groups

The first thing to do is hooking all these classes together so the MeetupManager can work. Open the MasterViewController.m, import the required header files, update the interface and declare the MeetupManager instance as follows:

1
2
3
4
5
6
7
8
#import "Group.h"
#import "MeetupManager.h"
#import "MeetupCommunicator.h"

@interface MasterViewController () <MeetupManagerDelegate> {
    NSArray *_groups;
    MeetupManager *_manager;
}

Later we’ll implement the methods as defined in MeetupManagerDelegate. But first let us instantiate the MeetupManager in the method of viewDidLoad:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
- (void)viewDidLoad
{
    [super viewDidLoad];
   
    _manager = [[MeetupManager alloc] init];
    _manager.communicator = [[MeetupCommunicator alloc] init];
    _manager.communicator.delegate = _manager;
    _manager.delegate = self;
   
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(startFetchingGroups:)
                                                 name:@"kCLAuthorizationStatusAuthorized"
                                               object:nil];
}

We are instantiating a new manager, then fill its communicator property with an new instance, and finally making the current view controller to keep track of any changes. The observer here captures the response of the user when the alert shows up to enable location services to call the method startFetchingGroups: to start fetching groups from server.

Next, open the “MasterViewController.m” and put the below to start fetching:

1
2
3
4
- (void)startFetchingGroups:(NSNotification *)notification
{
    [_manager fetchGroupsAtCoordinate:self.locationManager.location.coordinate];
}

As the view controller should conform to the MeetupManagerDelegate protocol. Implement the methods as follows:

1
2
3
4
5
6
7
8
9
10
- (void)didReceiveGroups:(NSArray *)groups
{
    _groups = groups;
    [self.tableView reloadData];
}

- (void)fetchingGroupsFailedWithError:(NSError *)error
{
    NSLog(@"Error %@; %@", error, [error localizedDescription]);
}

The “didReceiveGroups:” method will be called when the Meetup groups is grabbed from the Meetup server with an array instance returned. Here we simply reload the table view to display the new data.

Lastly, set up the methods of the table view by pasting the code below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _groups.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    Group *group = _groups[indexPath.row];
    [cell.nameLabel setText:group.name];
    [cell.whoLabel setText:group.who];
    [cell.locationLabel setText:[NSString stringWithFormat:@"%@, %@", group.city, group.country]];
    [cell.descriptionLabel setText:group.description];
   
    return cell;
}

The above code is very straightforward. We retrieve the groups from the array and fill the table cell with the group information.

Compile and Run

Now it’s time to test the app. Compile and run it in the Simulator (or a real iOS device if you’ve registered the iOS developer program). Setup the location of the Simulator to London, UK. You should get the Meetup groups similar to the below screenshot:

BrowseMeetup London

Tip: If you have no idea about testing location in iPhone Simulator, check out the Core Location tutorial.

Summary

We have covered in this tutorial many aspects of programming by adopting the right design pattern to work with JSON data. You should now have a hands-on experience on fetching and parsing JSON data.

The tutorial may be a bit difficult if you’re new to design pattern. However, this shouldn’t scare you away from using JSON in your app. The iOS SDK makes it so easy for your apps to work with JSON. In summary, you simply create a URL connection, fetch the JSON data and parse it by using the built-in NSJSONSerialization class.

You can find various free APIs (such as Kiva and TMDb) on the Internet. As an exercise, try to pick another API and develop another app for it. Say, you can develop a simple app and display a list of the most recent fundraising loans using Kiva API.

For your complete reference, you can download the full source code of the Xcode project here.

As always, please leave us comment and share your thought about the tutorial.

You May Like These:

Get Free Chapters of Our AppCoda Book

Learn iOS 7 Programming from Scratch

The Learn iOS 7 Programming from Scratch is a 400-page eBook written for beginners with little or even no programming experience. It is based on the tutorials of our popular programming course. The book starts with the basics and walks you through the process to create iOS apps using iOS 7 SDK and Xcode 5. Want to learn more? Check it out here and get two free chapters.


Build Your Own Game and Monetize on the Side

Learn how to develop your first iOS game and make money on the side. The starter kit will come with full source code of a memory game for both iPhone and iPad, as well as, a complete guide to explain how the code works. Check out the starter kit to learn more.


  • Pingback: JSON 데이터 Parsing | Prof. Kim's Lecture Notes

  • Osvaldo Cipriano

    Nice tutorial, again :)

  • vikram

    Nice tutorial with very good example…Thanx allot

  • Steve

    Nice tutorial!
    It works well, but rendering tableView takes so long.
    Is there any way to make rendering table view faster?

    • Ziad Tamim

      You can add a spinner to table view while data is loading, this way the user knows some work is going on.

      • Coração Tricolor

        Something is wrong with the code it shouldn’t take so long

  • Paulo

    Nice tutorial! Tks a Lot ;-)

  • LittlePeculiar

    Very nice tutorial. I love to come here and learn new stuff and more importantly, your clear and efficient design patterns.
    Thanks.

  • Pingback: How To Fetch and Parse JSON Data | iOS Programming Tutorial | MUSCLE CODER

  • frenzen

    ive been looking on every single site for so long for a good json tutorial and i refresh and see this awesome stuff

  • Thiodor

    One thing: the data is loaded but it is not displayed on the table view until you scroll it. Any ideas how to fix it or on which part of the code you must put [tableview reloadData].
    Thanks.

    • Juan Carlos Orrego

      This worked for me:

      dispatch_async(dispatch_get_main_queue(), ^{
      [self.tableView reloadData];
      });

      Based on the example, this should go in didReceiveGroups method of the master controller.

      • Daan

        Exactly what I needed ! Table redraw seemed to take forever…

  • ДЕНИС

    РАСИЯ ДЛЯ РУСКИХ!!

    • Name

      москва для москвичей!

  • Ly4S

    Thanks a lot for a great tutorial! I actually didn’t follow verbatim…I just needed to get the concept of how to interact with JSON APIs, so I could apply it to my own app, and this tutorial was a massive help. Can’t thank you enough for how easy you made the process for me. Keep up the great work you are doing!

  • Jordan

    Obviously an app like this won’t work without the internet. Is there an easy way to display to the user that “hey, you need a network connection to run this app”? Thanks for a great tutorial!

  • Arthur_Camara

    Thanks very, very much! Everything I found regarding iOS and JSON was related to old, non-native libraries. This is the best one I could find using native code!

  • Chuck Kelly

    ridiculously overly complicated for the objective of the tutorial…..can you ppl not just create a simple json parsing tutorial thats uses afnetworking and a tableview like everyone on the damn internet wants or is there some damn conspiracy on the internet amongst developers making actually relevant and practical tuts to protect their salary’s?

    • Aaron Wolverton

      Haha, this is mean, but I would have to agree.

    • cerberusss

      I found the described class hierarchy very useful to structure my app.

      Maybe it was overly complicated for you personally?

    • TooEasy

      Ridiculously complicated is an understatement

  • shane film

    Please provide code to modify the groupsFromJSon method within the GroupBuilder class access other data that like photo_link, or short name. Can you explain how to parse this data that is more deeply nested within the results dictionary? It appears that a great deal of the keyValues are part of NSArrays with data that isn’t accessible at the top level of results. This is is a phenomenal tutorial. Keep up the great work.

    • shane film

      Actually, I solved this by adding an NSArray to the Group.h file and then referenced it in the tableView of MasterViewController.m using valueForKey. Example: [group.category valueForKey:@"shortname"]). This returns the shortname of the category that is an NSArray that was added to the Group object.

  • Aaron Wolverton

    Is this tutorial supposed to display the detail/2nd view when you click on a meetup? It starts out with a master/detail application template, but my project doesn’t present a detail page when you click on a meetup/table cell from the root view.

  • NissiVM

    How the methods declared in MeetupCommunicatorDelegate can be called in the MeetupManager class if this class is not declared as the delegate of the class MeetupCommunicator?

    • TooEasy

      It is in the .h file

  • NissiVM

    Thank you very much for this tutorial, I’ve learned a lot from it! =]

  • lyao

    Really need another try to read this tutorial.

  • Gordon Chan

    Well I like this tutorial very much. I don’t think it is ridiculously overly complicated, instead, I am very much value on the concept of Communicator / Manager / Delegate. As it is a way to have good design of application.

    I am not good at application design and I start learning something like this, using Manager class to manage and work with custom built delegate. I look forward to see if you can provide another extend of this tutorial to show: when there is not only a single view controller, and different type of API call is performed in different view controller, do we still use single Manager class? or call the same Manager in different View controller?

  • Marc

    Thanks, Great Tuts. I like the clarity of delivery on Delegation and MVC

  • Maciek of Ozz

    Unnecessary overcomplicated, but useful :)

  • ttotto

    If there are many viewcontrollers to communicate Meetup, all of them should have meetup manager variables? Moreover all of them should be delegate of meetup manager?

  • yassine

    Hi,

    thanks for this tutorial, I have a question, can I work with this tutorial under Xcode 5 and iOS 7 ?!

    • SomeGuy

      Yes, this works with Xcode 5 and iOS 7. I have followed the tutorial successfully.

  • TooEasy

    What’s with the parameter type (NSData*)? I see it gets passed around but what is it exactly? The object “objectNotation” is an NSData type, where does the “objectNotation” parameter get set?

  • clau

    Hi! i have been tryin to get this to work all day but the selector on the NSNotificationCenter
    is not being called :(

  • Brian Fritch

    Like Chuck Kelly I thought AT FIRST that this approach was overly complicated. After I tried a simpler tutorial that didn’t work, I came back, adapted my app to this approach and it worked Perfectly!!

    Most of the tutorials only give part of the information you really need, or are so simple, it’s hard to expand the concept into a practical implementation. This is one of the few sets of tutorials that are actually well written and provide useful, real-world-like examples. Thank you!

  • Calin

    I might have missed it, but aren’t you supposed to tell MeetupCommunicator, that MeetupManager implements MeetupCommunicatorDelegate?

    I mean, how does [self.delegate receivedGroupsJSON:data]; in MeetupCommunicator know to call (void)receivedGroupsJSON:(NSData *)objectNotation in Meetupmanager?

    There should be something like self.communicator.delegate = self; somewhere in MeetupManager implementation, right?

    Thanks for the tutorial :)

  • Vishwa Iyer

    This was a very great tutorial, and I learned a lot from it. I just have one question though, is it possible to parse multiple apis using the same code above. If so, how is it possible?

  • Pingback: gpeoplehome.com

  • surya appkey

    I tried adding 1 page view controller between navigation controler and table view. but after that the list does not display data from a web service. can anyone help me?

  • byakuya

    Hi guys,

    I have been following many of your tutorials and I am totally fond of your work. Thanx a lot for what you are doing, it helps me a lot. I would like to perform this tutorial but for me the meetup API doesn’t work. Even if I am connected to meetup, when I try your request with my browser I have the following response from the server : {“details”:”API requests must be key-signed, oauth-signed, or accompanied by a key: http://www.meetup.com/meetup_api/docs/#authentication“,”problem”:”You are not authorized to make that request”,”code”:”not_authorized”}

    Can you help me please? Your tutorial is nearly exactly what I need to do for my own app, so it would help me a lot to perform the entire tutorial.

    Regards

  • Pingback: cause low testosterone

  • Pingback: Работа с JSON (iOs) « PeekabooApps

  • Alexandre Carmo

    Hello.
    Good tutorial.
    I have a problem using iOS 8, when I use iOS 7.1, this tutorial execute ok, I used emulator iOS 7.1 and device (iPhone 5s) using iOS 7.1, but when I use iOS 8 (emulator or device), dont display anything. There is not error message, and the json don’t return lines. What’s changed in iOS 8? Maybe localization ?

  • Alexandre Carmo

    Hello.
    Good tutorial.
    I have a problem using iOS 8, when I use iOS 7.1, this tutorial execute ok, I used emulator iOS 7.1 and device (iPhone 5s) using iOS 7.1, but when I use iOS 8 (emulator or device), dont display anything. There is not error message, and the json don’t return lines. What’s changed in iOS 8? Maybe localization ?

176 Flares Twitter 22 Facebook 101 Google+ 17 LinkedIn 36 176 Flares ×