ios - 如何解析这个 json 并在 uicollectionview 中显示

标签 ios objective-c json ios5 uicollectionview

<分区>


要求提供代码的问题必须表现出对所解决问题的最低限度理解。包括尝试过的解决方案、为什么它们不起作用,以及预期结果。另请参阅:Stack Overflow question checklist

关闭 9 年前

我是 iOS 的新手,已经在谷歌上搜索过这个,但我没有得到这方面的帮助。请帮我解决这个 JSON 解析以及如何在 UICollectionView 中显示这个图像。

{
    "wallpaper": [
        {
            "id": "31",
            "category": "animal",
            "title": "demo",
            "images": {
                "image_thumb": "http://192.168.1.7/ebook/uploaded_images/animal/demo/beach1377848613_thumb.jpg",
                "image1": "http://192.168.1.7/ebook/uploaded_images/animal/demo/beach1377848613_4.jpg",
                "image2": "http://192.168.1.7/ebook/uploaded_images/animal/demo/beach1377848613_5.jpg",
                "image3": "http://192.168.1.7/ebook/uploaded_images/animal/demo/beach1377848613_ipad.jpg",
                "image4": "http://192.168.1.7/ebook/uploaded_images/animal/demo/beach1377848613_android.jpg"
            },
            "hits": "0"
        },
        {
            "id": "33",
            "category": "abstract",
            "title": "demo 2",
            "images": {
                "image_thumb": "http://192.168.1.7/ebook/uploaded_images/abstract/demo2/beach1378075849_thumb.jpg",
                "image1": "http://192.168.1.7/ebook/uploaded_images/abstract/demo2/beach1378075849_4.jpg",
                "image2": "http://192.168.1.7/ebook/uploaded_images/abstract/demo2/beautifulnaturewallpapersforbackgroundhdwallpaper1378075850_5.jpg",
                "image3": "http://192.168.1.7/ebook/uploaded_images/abstract/demo2/DragonflyWallpaperRainForDekstopHD1378075850_ipad.jpg",
                "image4": "http://192.168.1.7/ebook/uploaded_images/abstract/demo2/hdwallpapers1080pwallpapers1378075850_android.jpg"
            },
            "hits": "0"
        },
}
}

最佳答案

希望这会有所帮助 ;-)

//Let's asume your json is loaded in this variable
NSString * myJsonString = @""; 

//convert string to dict
NSData * data = [myJsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError * error;
NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &error];

//Load array wallpapers (array of dictionaries)
NSArray * myArray = [dict objectForKey:@"wallpaper"];

现在您必须创建一个具有所需设计的 UITableViewCell(我们称它为 CustomCell),包括用于显示图像的 UIImageView。

然后在 TableView 数据源中:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [myArray count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CustomCell";
    CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        UIViewController *temporaryController = [[UIViewController alloc] initWithNibName:@"CustomCell" bundle:nil];
        cell = (CustomCell *)temporaryController.view;
    }

    NSDictionary * dict = [myArray objectAtIndex:i];

    NSString * image_thumb_url = [[dict objectForKey:@"images"] objectForKey:@"image_thumb"];

    //todo: download the image and display it into your cell here

    return cell;

}

关于ios - 如何解析这个 json 并在 uicollectionview 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18585714/

上一篇:ios - 具有水平 UIScrollView 的 UITableView 的视差效果

下一篇:适用于应用程序的 iOS 模拟器

相关文章:

ios - 如何在 Swift 4 中使静态库模块化?

ios - 尽管请求调用,但单击按钮时仍会暴露 UI

ios - 接触节点是 SpriteKit

json - CasperJs 从本地文件加载json数据

ios - 使用自动布局更改 UIView 的宽度

iOS9导致iPhone 5优化丢失

java - 如何在 json 文件中编辑和添加嵌套数据?

javascript - 解码/读取复杂文本文件的json部分

ios - 如何在设置 swift 3.0 时从我自己的应用程序重定向到“设置常规”选项卡

ios - 更改 Apple Watch 标签内的文本对齐方式