嗨,在我的应用程序中,我试图将json数据传递给我的tableview但它显示像这样发送给实例的无法识别的选择器
我有一个json数据,像这样。
"youtube_videos":[{"id":"1","name":"Little Flower Public School","youtube":"FFTi2Sl8hHw","link":"http://img.youtube.com/vi/FFTi2Sl8hHw/default.jpg"}]
这个json必须是youtube视频id和视频的缩略图现在我想在imageview中查看缩略图在tableview中查看但它给出了错误,如
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0xa72e290
这是用来获取json数据的代码。
-(void) retrieveData
{
NSURL * url = [NSURL URLWithString:getDataURL];
NSData * data = [NSData dataWithContentsOfURL:url];
json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
imgarray =[[NSMutableArray alloc]init];
for (int i=0; i<json.count; i++) {
NSString * dd = [[json objectAtIndex:i]objectForKey:@"youtube"];
NSString * sspp = [[json objectAtIndex:i]objectForKey:@"link"];
NSString * plae =[[json objectAtIndex:i]objectForKey:@"name"];
NSLog(@"%@",dd);
vv *myimg =[[vv alloc]initWithvideo:dd andtitle:plae andlink:sspp];
[imgarray addObject:myimg];
}
[self.mytableview reloadData];
My Json NSlog.
json :{
"youtube_videos" = (
{
id = 1;
link = "http://img.youtube.com/vi/FFTi2Sl8hHw/default.jpg";
name = "Little Flower Public School";
},
{
id = 2;
link = "http://img.youtube.com/vi/4oI7xC2wSpw/default.jpg";
name = "Little Flower Public School";
},
{
id = 3;
link = "http://img.youtube.com/vi/XCPKMWyF1Lo/default.jpg";
name = Function;
}
);
}
请告诉我如何解决这个问题,我已经卡在这里很长时间了。
谢谢。
我认为你应该像下面这样更新你的代码。因为你的JSON
看起来像Dictionary
。
-(void) retrieveData
{
NSURL * url = [NSURL URLWithString:getDataURL];
NSData * data = [NSData dataWithContentsOfURL:url];
id json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
imgarray = [[NSMutableArray alloc]init];
NSArray * respArray = [json objectForKey:@"youtube_videos"];
for (int i=0; i<respArray.count; i++)
{
NSString * dd = [[respArray objectAtIndex:i]objectForKey:@"youtube"];
NSString * sspp = [[respArray objectAtIndex:i]objectForKey:@"link"];
NSString * plae =[[respArray objectAtIndex:i]objectForKey:@"name"];
NSLog(@"%@",dd);
vv *myimg =[[vv alloc]initWithvideo:dd andtitle:plae andlink:sspp];
[imgarray addObject:myimg];
}
[self.mytableview reloadData];
}
不知道你的反应是什么。在json响应中获得单个或多个记录。试试下面的代码:
-(void) retrieveData
{
NSURL * url = [NSURL URLWithString:getDataURL];
NSData * data = [NSData dataWithContentsOfURL:url];
json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
imgarray =[[NSMutableArray alloc]init];
NSString * dd = [json objectForKey:@"youtube"];
NSString * sspp = [json objectForKey:@"link"];
NSString * plae =[json objectForKey:@"name"];
NSLog(@"%@",dd);
vv *myimg =[[vv alloc]initWithvideo:dd andtitle:plae andlink:sspp];
[imgarray addObject:myimg];
[self.mytableview reloadData];
}