這是一個響應,我想打印這個響應值到表視圖: 我張貼我的tableview代碼請檢查是否正確或錯誤。 我在這裏掙扎了2天,如果有人知道請回復我。我的表視圖創建但數組沒有顯示在表視圖,在控制檯中所有的數據都come.please幫我解決json解析
- (IBAction)search:(UIButton *)sender {
NSString *post =[[NSString alloc]initWithFormat:@"keyword=%@",[keyword text]];
NSLog(@"PostData: %@",post);
NSURL *url=[NSURL URLWithString:@"url"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
//[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
//NSError *error = [[NSError alloc] init];
NSError *error=nil;
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (!urlData) {
NSLog(@"Error: %@", [error localizedDescription]);
//return NO;
}
NSLog(@"Response code: %ld", (long)[response statusCode]);
if ([response statusCode] >=200 && [response statusCode] <300)
{
NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"Response ==> %@", responseData);
NSError *error = nil;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:urlData
options:NSJSONReadingMutableContainers error:&error];
NSLog(@"json ==> %@ ", dic);
name = [[NSMutableArray alloc]init];
username = [[NSMutableArray alloc]init];
for (id obj in dic) {
NSString *nameNameObj=[obj objectForKey:@"name"];
[name addObject:nameNameObj];
NSString *userNameObj=[obj objectForKey:@"username"];
[username addObject:userNameObj];
}
NSLog(@"name array is %@",name);
NSLog(@"username array is %@",username);
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [name count];
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [name count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"MYCustomCell";
MyCustomCellTableViewCell *cell = (MyCustomCellTableViewCell *)
[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle]
loadNibNamed:@"MyCustomCellTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
// apply setters for cell
cell.nameLbl.text=[name objectAtIndex:indexPath.row];
cell.usernameLbl.text=[username objectAtIndex:indexPath.row];
return cell;
}
[
{"id":60,"username":"test001","name":"name"},
{"id":61,"username":"test","name":""},
{"id":65,"username":"test002","name":"first"},
{"id":69,"username":"test003","name":"second"},
{"id":71,"username":"test004","name":"third"},
{"id":78,"username":"test005","name":"fourth"},
{"id":79,"username":"test006","name":"Name of the person"}
]
把你的代碼????? –
請在此處添加您的tableview委託方法。沒有這些,我們不能幫助你。 – Rashad