在CollectionView中显示的JSON数据

时间:2014-09-09 04:28:59

标签: ios json uicollectionview

我想在CollectionView中显示photoUrl图像。我怎么能这样做?

我的json数据如下所示:

{
    "albumName": "1",
    "photosCount": 8,
    "photoList": [
        {
            "photoId": 153,
            "photoName": "Photo Description",
            "type": "jpg",
            "size": "15kb",
            "photoURL": "http://1-dot-digiphoto-01.appspot.com/serve?blob-key=AMIfv94-p-RQhFsE4SRRozCxw49YlDIAUWdizg2MQ3h0-YIXXVNXEYVy_ACR7X9qNnATZ1-BW-w5nKk8H-EIpeiMl0766CHadLT6jb4cg225Wv5o2FUBIhaX5oB25l6185HTRvDar9cqz8MmL3WRiIduZMavRDZSXw",
            "dateTaken": "10-Jun-2010 06:11 PM",
            "latitude": "40.000072",
            "longitude": "116.390094"
        },
        {
            "photoId": 154,
            "photoName": "Photo Description",
            "type": "jpg",
            "size": "15kb",
            "photoURL": "http://1-dot-digiphoto-01.appspot.com/serve?blob-key=AMIfv96Vfva45jmoVlR7t0vGGuhKFffMeR_xttn9akPYrdO8Kwce8IRugHl8AeoDyWdgbt7uLv0YR3p2rqdPY1jREpAYyRz7OCHq90Vo8y4vdQ4z8PJLWTtRtqFogbOM_fLop3rfrqMy",
            "dateTaken": "10-Jun-2010 06:11 PM",
            "latitude": "40.000072",
            "longitude": "116.390094"
        }
    ]
}

3 个答案:

答案 0 :(得分:0)

将AFNetworking与此代码一起使用......

    //declares arrOfImages as NSMutableArray

    arrOfImages = [[NSMutableArray alloc]init];

    NSString *string =@"Your URL for JSON";

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
    {
        NSDictionary *diction = responseObject;
        NSArray *arr = [diction objectForKey:@"photoList"];
        for (NSDictionary *dict1 in arr) {

            [arrOfImages addObject:[dict1 objectForKey:@"photoURL"]];
        }
    }
    failure:^(AFHTTPRequestOperation *operation, NSError *error)
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert" message:[NSString stringWithFormat:@"%@",error.localizedDescription] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
        [hud hide:YES];
        [alert show];
    }];

现在您的NSMutableArray名为arrOfImages,其中包含URL列表。现在您可以将该URL设置为集合视图的图像视图。

答案 1 :(得分:0)

请查看此答案Creating a UICollectionView programmatically,您必须修改以下几项内容。

.h

中的

NSMutableArray *arrObject;
在词典中得到回复时, .m

if([yourDictionary[@"photoList"] isKindOfClass:[NSArray Class]])
{
    arrObject = yourDictionary[@"photoList"];
    [yourCollectionview reloadData];
}

然后你的代表将被召集。使用(https://github.com/rs/SDWebImage)下载图像异步。

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return arrObject.count;
}
    // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //create your cell and pass url
    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];

    [yourcell.imageView sd_setImagewithUrl:[arrObject[0] valueForKey:@"photoURL"]];
    return cell;
}

答案 2 :(得分:0)

您对Objective-C代码或Swift代码有疑问吗? 你粘贴的是一个JSON对象,其中一个名为 photoList 的数组作为其内容之一。你需要做的是获取 photoList

Objective-C示例代码如下:

假设 data 是JSON文件并且已导入NSJSONSerialization。 jsonDictionary是声明为NSDictionary的全局变量,而photoList是声明为NSArray的全局变量。

在viewDidLoad中:

jsonDictionary = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers];

photoList = [jsonDictionary objectForKey:@"photoList"];

在cellForItemAtIndexPath方法中:

NSString *path = [[self.photoList objectAtIndex:indexPath.row] objectForKey:@"photoURL"];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] initWithData:data];


cell.imageView.image = image;

希望这会有所帮助......