2016-04-29 82 views
0

我不知道如何從這個JSON數據中檢索特定的數據並在tableview中插入。如何在iOS的表格視圖中檢索json數據

{ 
"error": "ok", 
"data": [{ 
    "facebook_id": "1068644546541665", 
    "user_id": "95590e92-6c74-44d1-9e5c-5a10c6341ba3", 
    "name": "Sunil", 
    "trackingallowed": 1, 
    "trackingpermission": 1 
}, { 
    "facebook_id": "10311905002886663", 
    "user_id": "95ab5fc0-9c6d-4e3e-9cb2-bfa4d53e640f", 
    "name": "Saravanan", 
    "trackingallowed": 1, 
    "trackingpermission": 1 
}, { 
    "facebook_id": "998936463528828", 
    "user_id": "cd6e3c89-bc2d-45a9-8436-9603e1b4724b", 
    "name": "Suresh", 
    "trackingallowed": 1, 
    "trackingpermission": 1 
}] 

}

獲取通過列表數組中的值。

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
[friendsconnection_data appendData:data]; 
    NSError *localerror; 
    NSDictionary *parsedobject =[NSJSONSerialization JSONObjectWithData:data options:0 error:&localerror]; 
    NSLog(@"parsedobject1 %@",parsedobject); 
    ListArray = [parsedobject valueForKey:@"data"]; 

    NSLog(@"rrresults1 %lu",(unsigned long)ListArray.count); 
    NSLog(@"results1 %@",ListArray); 
} 

我得到EXC_BAD_ACCESS。我需要知道如何從JSON獲取特定數據。例如我只想得到facebookidname

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [self FriendsData]; 
    static NSString *TableIdentifier [email protected]"tableitem"; 
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:TableIdentifier]; 
    if(cell == nil) 
    { 
     cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableIdentifier]; 
    } 
    cell.textLabel.text = [[ListArray objectAtIndex:indexPath.row]objectForKey:@"name"]; 
    tableView.rowHeight = 300; 

    return cell; 
} 
+0

是什麼的cellForRowAtIndexPath該功能的工作高度? – Jaimish

+1

哪條線路導致崩潰?什麼是'FriendsData'方法,爲什麼從'cellForRowAtIndexPath'調用? – rmaddy

+0

不相關,但爲什麼每次調用cellForRowAtIndexPath時都要設置表視圖的'rowHeight'? – rmaddy

回答

-2

請使用此代碼。我認爲它幫你

NSError *json_parse_error; 
NSDictionary *responseDictionary=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&json_parse_error]; 
NSArray *user_info_array=[responseDictionary valueForKey:@"data"]; 

在表視圖委託

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *[email protected]"tableitem";; 
    cell=[tableView dequeueReusableCellWithIdentifier:TableIdentifier]; 

    if (cell==nil) { 
     cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:TableIdentifier]; 
    } 

    cell.textLabel.text = [[user_info_array objectAtIndex:indexPath.row] objectForKey:@"name"]; 

    cell.detailTextLabel.text = [[user_info_array objectAtIndex:indexPath.row] objectForKey:@"facebook_id"]; 

    return cell; 
} 

//設置表格視圖單元格

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 300.0; 
} 
+0

1)不要使用'valueForKey:'從字典中獲取對象。使用'objectForKey:'。 2)如果每行的高度相同,則不要實施'heightForRowAtIndexPath'。只需在'viewDidLoad'中設置'self.tableView.rowHeight = 300;'。 – rmaddy

+1

更重要的是,這個答案如何解決崩潰?不要只是將代碼張貼爲答案。解釋問題是什麼以及你的答案如何解決問題。 – rmaddy

+0

是的錶行視圖行高可以在viewDidLoad中設置 –