解析後、応答を配列に格納しています。その配列から、valueForKey を使用してキーと値のペアを選択し、別の配列に格納しています。uitableviewの各行にjson応答を表示する方法.Amは1行で応答全体を取得しています
arrTotalRequests=[[NSMutableArray alloc]init];
str= [[NSUserDefaults standardUserDefaults] valueForKey:@"user_id"];
NSString *strJson=[NSString stringWithFormat:@"userId=%@",str];
NSString *strlength=[NSString stringWithFormat:@"%d",[strJson length]];
NSURL *url=[NSURL URLWithString:@"url"];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
[request addValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
[request addValue:strlength forHTTPHeaderField:@"Content-Length"];
NSData *requestData=[strJson dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:requestData];
[request setHTTPMethod:@"POST"];
NSError *err;
NSData *responseData=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&err];
NSString *strResponse=[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"response is %@",strResponse);
// [[NSURLConnection alloc]initWithRequest:request delegate:self];
arr1=[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:&err];
arr2=[arr1 valueForKey:@"result"];
arrTotalRequests = [arr2 valueForKey:@"description"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [arrTotalRequests count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier=@"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"identifier"];
}
NSString *str2=[NSString stringWithFormat:@"%@", [arrTotalRequests valueForKey:@"description"]];
NSLog(@"new desc is %@",str2);
cell.textLabel.text=str2;
}