如何解析数据...并填充tableview中的数据

时间:2016-03-15 07:37:42

标签: ios

         {
                "COMPETITION_CODE":"UCC0000003",
                "MATCH_CODE":"DMSC1160026829B543400022",
                "MATCH_DATE":"1/18/2016 12:00:00 AM",
                "A_TEAMNAME":"UTTAR PRADESH",
                "B_TEAMNAME":"DELHI",
                "A_TEAMSHORTNAME":"UPCA",
                "B_TEAMSHORTNAME":"DHL",
                "BATTING_TEAMNAME":"UTTAR PRADESH",
                "MATCH_TYPE":"MSC116",
                "HOME_TEAM":"UTTAR PRADESH",
                "FIRSTINNINGS_SHORTNAME":"DHL",
                "FIRSTINNINGS_SCORE":"158/6",
                "FIRSTINNINGS_TOTAL":"158",
                "FIRST_INNINGSWICKET":"6",
                "FIRST_INNINGSOVERS":"20.0",
                "SECOND_INNINGSSHORTNAME":"UPCA",
                "SECOND_INNINGSSCORE":"159/7",
                "SECOND_INNINGSTOTAL":"159",
                "SECOND_INNINGSWICKET":"7",
                "SECOND_INNINGSOVERS":null,
                "THIRD_INNINGSSHORTNAME":"",
                "THIRD_INNINGSSCORE":"0/0",
                "THIRD_INNINGSTOTAL":"0",
                "THIRD_INNINGSWICKET":"0",
                "THIRD_INNINGSOVERS":"",
                "FOURTH_INNINGSSHORTNAME":"",
                "FOURTH_INNINGSSCORE":"0/0",
                "FOURTH_INNINGSTOTAL":"0",
                "FOURTH_INNINGSWICKET":"0",
                "FOURTH_INNINGSOVERS":"",
                "FIRST_INNINGSLOGO":null,
                "SECOND_INNINGSLOGO":null,
                "RUN_RATE":"8.1",
                "REQUIREDRUN_RATE":"0.0",
                "MATCH_RESULT":"Won by 3 Wickets",
                "Player_TeamList":[
                   {
                       "PLAYER_NAME":"PRAVEEN KUMAR",
                       "PLAYER_ROLE":"Bowler",
                       "TEAM_NAME":"UTTAR PRADESH",
                       "TEAM_CODE":"TEA0000001",
                       "PLAYER_ORDER":"1"
                   },
                   {
                       "PLAYER_NAME":"AKSHDEEP NATH",
                       "PLAYER_ROLE":"Batsman",
                       "TEAM_NAME":"UTTAR PRADESH",
                       "TEAM_CODE":"TEA0000001",
                       "PLAYER_ORDER":"1"
                   },
                   {
                       "PLAYER_NAME":"AMIT MISHRA",
                       "PLAYER_ROLE":"Bowler",
                       "TEAM_NAME":"UTTAR PRADESH",
                       "TEAM_CODE":"TEA0000001",
                       "PLAYER_ORDER":"1"
                   },
                   {
                       "PLAYER_NAME":"PIYUSH CHAWLA",
                       "PLAYER_ROLE":"All Rounder",
                       "TEAM_NAME":"UTTAR PRADESH",
                       "TEAM_CODE":"TEA0000001",
                       "PLAYER_ORDER":"1"
                   },
                   {
                       "PLAYER_NAME":"ANKIT RAJPOOT",
                       "PLAYER_ROLE":"Bowler",
                       "TEAM_NAME":"UTTAR PRADESH",
                       "TEAM_CODE":"TEA0000001",
                       "PLAYER_ORDER":"1"
                   }]
                }

我试过解析得到serviceResponse中的响应值,但需要在tableview索引路径中填充数组和字典值

任何人都可以帮助我解决这个问题

NSURL *url = [NSURL URLWithString:[baseURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
   NSURLResponse *response;
NSError *error;
NSData *responseData =[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSDictionary *serviceResponse=[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];

先谢谢

1 个答案:

答案 0 :(得分:0)

您的问题不明确,根据您提供的回复,您似乎必须在tableView单元格上显示以下属性,

{
   "PLAYER_NAME":"PRAVEEN KUMAR",
   "PLAYER_ROLE":"Bowler",
   "TEAM_NAME":"UTTAR PRADESH",
   "TEAM_CODE":"TEA0000001",
   "PLAYER_ORDER":"1"
}

您应该将数据解析为

NSDictionary *serviceResponse=[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];

NSArray *arrayOfPlayer_TeamList = serviceResponse[@"Player_TeamList"];

解析响应后,在主线程上重新加载tableView。

修改/更新

如果你必须使用整个响应,那么只需解析已经解析的serviceResponse字典中的响应,使字典全局并在你的cellForRowAtIndexPath中:

  1. 当您必须显示匹配日期时,

    NSString *matchDate = serviceResponse[@"MATCH_DATE"];
    
  2. 当你必须显示每个单元格不同的“PLAYER_NAME”时,

    NSString *playerName = serviceResponse[@"Player_TeamList"][indexPath.row][@"PLAYER_NAME"];
    
  3. 希望这会对你有所帮助。

    Edit2 / Update2:

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
        return [_dictTeamData[@"Player_TeamList"] count];
    }
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        static NSString *cellIdentifier = @"cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
        //Team Name & Batting Team Name are outside Player_TeamList
    
        //Team Name
        UILabel *teamName = (UILabel*) [cell viewWithTag:10];
        teamName.text = _dictTeamData[@"A_TEAMNAME"];
    
        //Batting Team Name
        UILabel *battingTeamName = (UILabel*) [cell viewWithTag:20];
        battingTeamName.text = _dictTeamData[@"BATTING_TEAMNAME"];
    
    
        //Player Name & Player Role inside Player_TeamList
        //Player Name
        UILabel *playerName = (UILabel*) [cell viewWithTag:30];
        playerName.text = _dictTeamData[@"Player_TeamList"][indexPath.row][@"PLAYER_NAME"];
    
        //Player Role
        UILabel *playerRole = (UILabel*) [cell viewWithTag:40];
        playerRole.text = _dictTeamData[@"Player_TeamList"][indexPath.row][@"PLAYER_ROLE"];
        return cell;
    }
    

    结果是:

    enter image description here