如何显示JSON中图像的水平滚动视图

时间:2017-01-24 10:22:35

标签: ios objective-c json

我的JSON回复

 {"result":0,"data":[{"bannerslider_id":"1","img":"http:\/\/whatsinthestreet.com\/media\/bannerslider\/images\/slider1.jpg"},{"bannerslider_id":"2","img":"http:\/\/whatsinthestreet.com\/media\/bannerslider\/images\/slider2.jpg"},{"bannerslider_id":"3","img":"http:\/\/whatsinthestreet.com\/media\/bannerslider\/images\/slider3.jpg"},{"bannerslider_id":"4","img":"http:\/\/whatsinthestreet.com\/media\/bannerslider\/images\/slider4.jpg"}]}

我写了这样的代码

(void)viewDidAppear:(BOOL)animated{
    [[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:@"http://whatsinthestreet.com/allbanners.php"] completionHandler:^(NSData * _Nullable data ,NSURLResponse * _Nullable response,NSError * _Nullable error){
        NSLog(@"response %@",response);
        NSLog(@"data %@",data);
        NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        NSLog(@"dict%@",dict);
       NSArray *array;
        array=[dict valueForKey:@"data"];
       NSLog(@"%@",array);

我得到了回复

 dict{
    data =     (
                {
            "bannerslider_id" = 1;
            img = "http://whatsinthestreet.com/media/bannerslider/images/slider1.jpg";
        },
                {
            "bannerslider_id" = 2;
            img = "http://whatsinthestreet.com/media/bannerslider/images/slider2.jpg";
        },
                {
            "bannerslider_id" = 3;
            img = "http://whatsinthestreet.com/media/bannerslider/images/slider3.jpg";
        },
                {
            "bannerslider_id" = 4;
            img = "http://whatsinthestreet.com/media/bannerslider/images/slider4.jpg";
        }
    );
    result = 0;
}

如何从这些值访问图像以获取图像的水平滚动视图?

1 个答案:

答案 0 :(得分:0)

这应该适合你。

-(void)viewDidAppear:(BOOL)animated{
    [[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:@"http://whatsinthestreet.com/allbanners.php"] completionHandler:^(NSData * _Nullable data ,NSURLResponse * _Nullable response,NSError * _Nullable error){
        NSLog(@"response %@",response);
        NSLog(@"data %@",data);
        NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        NSLog(@"dict%@",dict);
       NSArray *array;
        array=[dict objectForKey:@"data"];
       NSLog(@"%@",array);
       for(id obj in array)
       {
            NSString *urlStr=[obj objectForKey:@"img"];
        }
}