string
{ "name":{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address":
{
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
},
"phoneNumber":
[
{
"type": "home",
"number": "212 555-1234",
"home":"sdfsdfdsf"
},
{
"type": "fax",
"number": "646 555-4567",
"home":"6666"
}
{
"type": "sun",
"number": "646 555-4567",
"home":"lopppp"
}
]
}
}
//////////////////////// this is how we parse this JSON
- (void)viewDidLoad {
jsonArray= [[NSMutableArray alloc] init] ;
[super viewDidLoad];
responseData = [[NSMutableData data] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://58.185.167.53/rahul/Json/Number.json"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
label.text = [NSString stringWithFormat:@"Connection failed: %@", [error description]];
}
- (void)addRowToLogWindow:(id)data {
label.text = [NSString stringWithFormat:@"Adding data: %@%@", data, label.text]; // adding new row + 2x new line "
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
NSError *error;
SBJSON *json = [[SBJSON new] autorelease];
NSDictionary *data = (NSDictionary *) [json objectWithString:responseString error:nil];
NSDictionary *menu = (NSDictionary *) [data objectForKey:@"name"];
NSArray *items = (NSArray *) [menu objectForKey:@"phoneNumber"];
int ndx;
for (ndx = 0; ndx< items.count; ndx++) {
stream = (NSDictionary *)[items objectAtIndex:ndx];
NSLog(@"This is the title of a stream: %@", [stream valueForKey:@"type"]);
[jsonArray addObject:stream];
}
NSLog(@" aray is %@",jsonArray);
NSLog(@" sterab is %@",[stream valueForKey:@"home"]);
[tableview reloadData];
NSLog(@" json coutn is %d",[jsonArray count]);
NSString *luckyNumbers = [json objectWithString:responseString error:&error];
[responseString release];
if (luckyNumbers == nil)
label.text = [NSString stringWithFormat:@"JSON parsing failed: %@", [error localizedDescription]];
else {
NSMutableString *text = [NSMutableString stringWithString:@"Lucky numbers:\n"];
label.text = text;
}
}
#pragma mark tabel
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//if (searching)
//return 1;
//else
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@" 43534535353 %d",[jsonArray count]);
return [jsonArray count];
}
//////////// WORKING CELL BIGGER SIZE CODE///////////
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
/* if(indexPath.row == 0)
return 85;
*/
return 55;
}
////////////// ENDS HERE/////////////
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//UITableView *tableView;
UITableViewCell *cell;
static NSString *CellIdentifier = @"Cell";
//self.tableView.frame = CGRectMake(0,searchBar.bounds.size.height,320,480);
cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil ) {
NSLog(@" inside");
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(10.0, 5.0, 220.0, 15.0)] autorelease];
mainLabel.tag =33;
// mainLabel.font = [UIFont systemFontOfSize:14.0];
[mainLabel setFont:[UIFont boldSystemFontOfSize:[UIFont smallSystemFontSize]]];
mainLabel.textAlignment = UITextAlignmentLeft;
mainLabel.textColor = [UIColor blackColor];
mainLabel.highlightedTextColor = [UIColor greenColor];
//mainLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:mainLabel];
mainLabel.backgroundColor=[UIColor clearColor];
}
mainLabel.text = [[jsonArray objectAtIndex:indexPath.row] objectForKey:@"number"];
NSLog(@" dicst 5@",stream);
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
return cell;
}
No comments:
Post a Comment