More Complex JSON endpoint

Make sure you have JSON Framework installed first.

This is a continuation from First JSON Iphone application

So the last entry went into describing how we do very basic JSON. Now lets get into something a bit more complex. Here is what my array looks like in PHP.

$array = array(
        array(
                'title' => 'Brown Dog',
                'img'   => 'http://iphone.zcentric.com/files/1.jpg',
        ),
        array(
                'title' => 'Black Dog',
                'img'   => 'http://iphone.zcentric.com/files/2.jpg',
        ),
        array(
                'title' => 'Two Dogs',
                'img'   => 'http://iphone.zcentric.com/files/3.jpg',
        ),
        array(
                'title' => 'Girl',
                'img'   => 'http://iphone.zcentric.com/files/4.jpg',
        ),
);

So you can see it is a multi-dimensional array in PHP. In the main array are 4 sub-arrays that each have a title and a img key. The title is the title of the image and the img is a URL to the image. So we can pretty much use our example almost line for line to do this more complex array now

So first open up RootViewController.m and find the line where we inited the JSON URL. All you are doing is changing the endpoint

NSURL *jsonURL = [NSURL URLWithString:@"http://iphone.zcentric.com/test-json2.php"];

Before it was using test-json.php and now we are using test-json2.php.

Now go down to cellForRowAtIndexPath and remove the cell.text line that is there now. You want the cell text to look like this now

	NSDictionary *itemAtIndex = (NSDictionary *)[self.jsonArray objectAtIndex:indexPath.row];
	cell.text = [itemAtIndex objectForKey:@"title"];

So you can see what we are doing is grabbing a dictionary off the array at a certain index and then in that dictionary we are putting the title in for the cell text.

If you build and go now you should see this.

As always you can download the source here.

About mike
Currently works for OpenSky as a Senior Linux Admin. He has a wonderful wife Thanuja and 2 great dogs. His major side project is Photoblog.

Comments

2 Responses to “More Complex JSON endpoint”
  1. Jovany says:

    UpEp4z71lv5Ih

Trackbacks

Check out what others are saying about this post...
  1. [...] This is a continuation of More Complex JSON endpoint [...]



Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!