//// MasterViewController.h// JsonDemo_DbSelect//// Created by apple on 20/02/13.// Copyright (c) 2013 apple. All rights reserved.//
#import <UIKit/UIKit.h>
@class DetailViewController;
@interface MasterViewController : UITableViewController@property(nonatomic,retain)NSMutableArray *mydata;@property (strong, nonatomic) DetailViewController *detailViewController;
@end
MasterViewController.m
//// MasterViewController.m// JsonDemo_DbSelect//// Created by apple on 20/02/13.// Copyright (c) 2013 apple. All rights reserved.//
#import "MasterViewController.h"
#import "DetailViewController.h"
@interface MasterViewController () { NSMutableArray *_objects;}@end
@implementation MasterViewController@synthesize mydata;
-(void)getJsonData:(NSData *)data{ NSError *error; mydata=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; NSLog(@"%@",mydata); [self.tableView reloadData];
}
-(void)parseJson{ NSString *str=[NSString stringWithFormat:@"http://localhost/App1/json_select.php"]; NSURL *url=[NSURL URLWithString:str]; NSData *data=[NSData dataWithContentsOfURL:url]; [self getJsonData:data];
}
- (void)viewDidLoad{ [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib. self.title=@"Db Json Retrive"; [self parseJson]; }
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1;}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [mydata 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) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; }
//NSDate *object = _objects[indexPath.row]; //cell.textLabel.text = [object description]; NSDictionary *d=[mydata objectAtIndex:indexPath.row]; cell.textLabel.text=[d objectForKey:@"name"]; cell.detailTextLabel.text=[d objectForKey:@"sex"]; return cell;}
- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}
@end
MasterViewController.xib
json_select.php
<?php
$host = "localhost"; $user = "root"; $pass = ""; $database = "IosProj";
$con = mysql_connect($host, $user, $pass) or die("Could not connect to host."); mysql_select_db($database, $con) or die("Could not find database.");
$sql = mysql_query("SELECT * FROM Register");$rows = array();while($row = mysql_fetch_assoc($sql)) { $rows[] = $row;}//print json_encode($rows);echo(json_encode($rows));
?>
phpMyadmin:
No comments:
Post a Comment