【发布时间】:2016-05-03 10:08:33
【问题描述】:
我是 IOS 新手,我需要将 json 获取的数据显示到 uitableview 我的 json 模式如下所示。
{"output":[{"id":{"php":"1"},"name":{"php":"ramkumar"},"image":{"1":"http:\/\/www.easyteach.gr\/users\/tutor\/1432636961.jpg"}},{"id":{"php":"2"},"name":{"php":"rakshi"},"image":{"2":"http:\/\/www.easyteach.gr\/users\/tutor\/1440371842.jpg"}},{"id":{"android":"1"},"name":{"android":"Vijayan"},"image":{"3":"http:\/\/www.easyteach.gr\/users\/tutor\/1432636961.jpg"}},{"id":{"android":"2"},"name":{"android":"vishali"},"image":{"4":"http:\/\/www.easyteach.gr\/users\/tutor\/1440371842.jpg"}},{"id":{"android":"3"},"name":{"android":"Niranjan"},"image":{"5":"http:\/\/www.easyteach.gr\/users\/tutor\/1432636961.jpg"}}]}
viewdidload 编码:
我在 viewdidload 本身中将 json url 创建为数组格式,并在 nslog 中成功显示,但在 tableview 中可能会失败。
if(data){
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options: kNilOptions error:&error];
NSLog(@"%@",json);
nameArray = [json valueForKeyPath:@"output.name"];
php = [nameArray valueForKey:@"php"];
}
[_tabledata reloadData];
Tableview 代表:
-(NSInteger)numberOfSectionsInTableView:(UITableView *) tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger)section{
return [php count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"table" forIndexPath:indexPath];
cell.iteam.text=[php objectAtIndex:indexPath.row];
return cell;
}
-(void)tableView: (UITableView *) tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
}
【问题讨论】:
-
控制台是否有任何错误?
-
我认为这是因为您的
nameArrayvar 是nil因为您的 json 中没有任何路径是output.name并且您的phpvar 也将为零。 -
是的,我遇到了类似 thread@sandeeptomar 的错误
-
它在 nslog name 数组中的显示值来了,php 也来了@tx2
-
NSLogphp你会明白的
标签: ios json uitableview