tags:

views:

61

answers:

2

I'm trying to create an iphone app which grabs a JSON string, parses it and displays a list of items in at table view.

I'm also attempting to use an Model-View-Controller (MVC) architecture for my project.

My question is in 2 parts:

1) How do I structure my different files to conform to this standard (MVC) ?

2) (more general) I've been going through a lot of XML parsing examples, and they seem to implement standard methods such as 'requestDidFinishLoad', 'setActiveProperty', etc... How can I find out exactly what methods I need to implement to successfully send a request and parse a JSON string?

A: 

Apple has great documentation on MVC. See this link.

The basic idea is to separate your app into the part responsible for displaying the data (the view), the data itself (the model) and the interface between the two (the controller).

In your case, if you are just parsing and displaying JSON and don't need to save or edit the information, you can cut out the model and do all the work in the controller. Just make a UITableViewController subclass that parses the JSON into an array and uses the array as the data source for the table view. If storing the data permanently is important to you than you want to look at something like plists or CoreData.

Jergason
Thanks the response. Maybe, i should give you a little more info about what I'm trying to design. Basically, i have an object called a 'flag' with 2 attributes (id, message) and a timeline (a list of flags). The flag list is going to be passed to me through an API call. So ideally, I would like to have a separate class file that handles all the JSON parsing, creates an array of flags and passes it to the tableView. So, should i be implementing the JSONobject as some kind of data source instead?
dpigera
+2  A: 

Your model for your table view will likely be an NSArray or NSDictionary instance. An array is easier for the purposes of demonstration.

The json-framework on Google Code will let you pull a JSON array into an NSArray very easily.

As an example, let's say your table view controller has a retained NSArray property called items.

Then the JSON object here:

{
    "items" : [
        "item1",
        "item2", 
        ...
        "itemN"
    ]
} 

Can be poured into an array as follows:

SBJSON *jsonParser = [[SBJSON alloc] init];
NSDictionary *jsonDictionary = (NSDictionary *) [jsonParser objectWithString:jsonString error:nil];
self.items = (NSArray *) [jsonDictionary objectForKey:@"items"];
[jsonParser release];

Your table view data source delegate just pulls out objects from the items array, e.g.:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [items count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // instantiate or dequeue cell...

    // label cells with JSON item names
    cell.textLabel.text = [items objectAtIndex:indexPath.row];
}
Alex Reynolds
Just tried this out.. works like a charm. Thanks for the response!
dpigera