【发布时间】:2017-04-11 13:15:50
【问题描述】:
我的 JSON 数据如下所示:
{
"success": true,
"data": [
{
"playerId": "j_mohammed",
"playerName": "Jason Mohammed",
"playerShortName": "Jason Mohammed",
"PlayingRole": 3,
"Credit": 85,
"Player_Points": 45,
"TeamName": "wi",
"TeamColor": "1"
},
{
"playerId": "s_gabriel",
"playerName": "Shannon Gabriel",
"playerShortName": "ST Gabriel",
"PlayingRole": 4,
"Credit": 80,
"Player_Points": 0,
"TeamName": "wi",
"TeamColor": "1"
},
{
"playerId": "s_hope",
"playerName": "Shai Hope",
"playerShortName": "S Hope",
"PlayingRole": 1,
"Credit": 85,
"Player_Points": 0,
"TeamName": "wi",
"TeamColor": "1"
},
{
"playerId": "e_lewis",
"playerName": "Evin Lewis",
"playerShortName": "Evin Lewis",
"PlayingRole": 3,
"Credit": 85,
"Player_Points": 281,
"TeamName": "wi",
"TeamColor": "1"
},
{
"playerId": "m_hafeez",
"playerName": "Mohammad Hafeez",
"playerShortName": "Mohammad Hafeez",
"PlayingRole": 2,
"Credit": 90,
"Player_Points": 0,
"TeamName": "pak",
"TeamColor": "4"
},
{
"playerId": "f_ashraf",
"playerName": "Fahim Ashraf",
"playerShortName": "Fahim Ashraf",
"PlayingRole": 3,
"Credit": 85,
"Player_Points": 0,
"TeamName": "pak",
"TeamColor": "4"
}
]
}
我已经将此 JSON 解析到我的应用程序中。 我的应用程序的视图如下所示:
因为它显示我在视图顶部有 4 个按钮:
我在 ViewController 中使用了 TableView 来解析数据:
这就是我所做的。现在我的主要问题是,当我单击按钮“WK”时,我只想在 tableview 中显示来自 JSON 的检票员列表,当我单击“BAT”时,击球手的列表等等。我现在已经完成了解析,它显示了所有来自 JSON 的数据,不知道如何在单击按钮时在 TableView 中显示选定的数据。
无论球员是投球手、击球手还是三柱门守门员,它都以 JSON 格式存储为 "PlayingRole"。
所以,如果玩家是Wicket keeper its role is:1
All rounder:2
Batsman:3
Bowler:4
这是我完成的代码:
NSString *mainurl = [NSString stringWithFormat:@"http://192.168.1.102/redbull11/webservices/api/getMatcheSquad/“];
NSString *playerurl = [NSString stringWithFormat:@"%@%@",mainurl,match_id2];
NSURL *url=[NSURL URLWithString:playerurl];
NSLog(@"mainurl: %@",url);
NSURLSessionConfiguration *csc=[NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *csession = [NSURLSession sessionWithConfiguration:csc delegate:nil delegateQueue:[NSOperationQueue mainQueue]];
NSURLSessionDataTask *ctask=[csession dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error){
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
getsuccess = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
dispatch_async(dispatch_get_main_queue(), ^{
NSString *success =[getsuccess valueForKey:@"success"];
if([success boolValue] == YES)
{
NSArray *arr =[getsuccess valueForKey:@"data"];
for ( NSDictionary *dic2 in arr) {
NSMutableArray *temp =[[NSMutableArray alloc]init];
[temp addObject:[dic2 valueForKey:@"playerShortName"]];
[temp addObject:[dic2 valueForKey:@"Credit"]];
[temp addObject:[dic2 valueForKey:@"Player_Points"]];
[temp addObject:[dic2 valueForKey:@"TeamName"]];
[temp addObject:[dic2 valueForKey:@"PlayingRole"]];
[getLoc addObject:temp];
}}
NSLog(@"%@",getLoc);
[_playerselect reloadData];
[MBProgressHUD hideHUDForView:self.view animated:YES];
});
});
}];
[ctask resume];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
selectTeamCell *cell = [tableView dequeueReusableCellWithIdentifier:@"names" forIndexPath:indexPath];
NSMutableArray *arr =[[NSMutableArray alloc]init];
arr = [getLoc objectAtIndex:indexPath.row];
NSLog(@"%ld",(long)indexPath.row);
cell.playerName.text = [arr objectAtIndex:0];
cell.playerCredit.text = [[arr objectAtIndex:1]stringValue];
cell.playerPoints.text = [[arr objectAtIndex:2]stringValue];
cell.teamName.text = [arr objectAtIndex:3];
return cell;
}
请帮忙。提前谢谢你。
【问题讨论】:
-
还有...你的代码是什么?
-
@Maulik Desai Parse json 根据您的要求创建两个数组,一个用于击球手,一个用于守门员附加适当的数据。并相应地获取它们。
-
@AlejandroIván 看到我已经编辑了问题。
-
检票员钥匙在哪里?
-
@DishantRajput in JSON "PlayingRole": 1 表示 wicketkeeper。
标签: ios objective-c json uitableview