【问题标题】:how to parse this json and show in uicollectionview [closed]如何解析这个json并在uicollectionview中显示[关闭]
【发布时间】:2013-09-03 06:38:25
【问题描述】:

我是 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"
        },
}
}

【问题讨论】:

  • 您将 Wallpaper 作为一个数组,其中包含多个字典。每个字典都包含 id 作为字符串、类别为字符串、标题为字符串、命中为字符串和图像数组。
  • 在我看来你应该从这里开始:mobile.tutsplus.com/tutorials/iphone/iphone-json-twitter-api
  • 你在看我的代码吗......

标签: ios objective-c json ios5 uicollectionview


【解决方案1】:

希望这会有所帮助;-)

//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;

}

【讨论】:

    【解决方案2】:
    NSURL * url=[NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.0.5/getappdata.php"]];
    NSData * data=[NSData dataWithContentsOfURL:url];
    
    NSError * error;
    
    //Get json data in Dictionary
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &error];
    
    NSLog(@"%@",json);
    

    试试这段代码解析json...

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-21
    • 1970-01-01
    • 2015-06-09
    • 2012-05-01
    • 2021-06-13
    • 2022-01-06
    • 1970-01-01
    相关资源
    最近更新 更多