Saturday 21 July 2012

JSON using for Web Service in Xcode

Sample Project Download Link :  http://www.mediafire.com/?yqeydj9lv10l97u 

JSON Files Download Link : http://www.mediafire.com/?iw9p1bap515cojn


JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.
In this tutorial you would learn to load, parse and show JSON!

Start Xcode and create a new project based on the 'Navigation-based Application' template.
create-project.png
Give it a name and choose 'iPhone' as the 'Device Family'. Create a new group into your project folder and call it 'JSON'.
Download de compressed 'JSON' folder from the attachments. Uncompress the downloaded .zip file and drag the files in the JSON group, inside your project folder. Set the option 'Copy items into destination group's folder (if needed)' checked. Click on 'Finish'.
add-json.png
And now it's time to write code.


Open the RootViewController.h, and change the code like below. Add an NSDictionary to hold the tabledata.
// RootViewController.h
#import <UIKit/UIKit.h> 
@interface RootViewController : UITableViewController 
{
 NSArray *tableData;
} 
@property (nonatomic, retain) NSArray *tableData;
 @end


Open the RootViewController.m, and change the code like below. First import the "JSON.h" and synthesize the NSDictionary. Then download the JSON and write his contents in your TableViewcontroller.


// Add this line on top of your RootViewController.m
#import "JSON.h" 

// And directly under the @implementation RootviewController add the following line
@synthesize tableData;

When the view will appear we will download the JSON. Change the method viewWillAppear as below.

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];


// Download JSON
NSString *jsonString = [NSString stringWithContentsOfURL stringWithContentsOfURL:[NSURL URLWithString:@"http://altinkonline.nl/temp/xcode-and-parsing-json.json"] encoding
encoding:NSStringEncodingConversionAllowLossy error error:nil];


// Create parser 
 SBJSON *parser = [[SBJSON alloc] init];
NSDictionary *results = [parser objectWithString:jsonString error:nil];
[parser release], parser = nil;


// Set tableData
[self setTableData:[results objectForKey:@"items"]];
}


Now we have the parsed JSON we will show the JSON on the UITableView. Change the functions in your "RootViewController.m" as below.


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of items in tabledata
return [tableData count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";   

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


if (cell == nil) 
{
// Change UITableViewCellStyle 
 cell = [[[UITableViewCell alloc] initWithStyle initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier reuseIdentifier:CellIdentifier] autorelease];
} 

// Get item from tableData
NSDictionary *item = [tableData objectAtIndex:[indexPath row]];


// Set text on textLabel
[[cell textLabel] setText:[item objectForKey:@"title"]];


// Set text on detailTextLabel
[[cell detailTextLabel] setText:[item objectForKey:@"description"]]
 return cell;
}

// When view is unloaded we don't need the tableData because we reload them in the ViewWillAppear- (void)viewDidUnload
{
[self setTableData:nil];
[super viewDidUnload];
}

// Release tableData on exit

- (void)dealloc
{[tableData release];
[super dealloc];
}

No comments: