迅速从GIPHY API中解析JSON信息

发布于 2025-02-05 21:29:37 字数 2046 浏览 2 评论 0原文

我目前正在尝试创建一个iOS应用程序,该应用在表观视图中显示趋势GIF,我一直疯狂地尝试从GIPHY API访问Image URL。我目前正在使用Alamo Fire访问数据,这就是我的代码到目前为止的外观:

import UIKit
import Alamofire

class ViewController: UIViewController {
    
    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        fetchGiphyData()

    }
    
    func fetchGiphyData() {
    
        let request = AF.request("https://api.giphy.com/v1/gifs/trending?api_key=zONZGFIbbvYDBjLEBlgKxLjbsClfmptw&limit=25&rating=pg-13")
        
        request.responseDecodable(of: GiphyData.self) { (response) in
            guard let gifs = response.value else { return }
            print(gifs.height)
            
        }
    }
}

这是我的数据收集的方式:JSON信息:

import UIKit

struct Data: Codable {
    var data: [ImageInfo]
}

struct ImageInfo: Codable {
    var images: ImageTypes
}

struct ImageTypes: Codable {
    var original: GiphyData
}

struct GiphyData: Codable {
    var url: String
    var height: String
}

struct GiphyImages: Codable {
    var giphyImages: [GiphyData]
}

这是Giphy的API信息:

{
"data":[
{
"type":"gif"
"id":"If1a3H7OHi8vmiCTpW"
"url":"https://giphy.com/gifs/nhl-draft-nhl-2019-prospects-If1a3H7OHi8vmiCTpW"
"slug":"nhl-draft-nhl-2019-prospects-If1a3H7OHi8vmiCTpW"
"bitly_gif_url":"https://gph.is/g/Zdx6J5o"
"bitly_url":"https://gph.is/g/Zdx6J5o"
"embed_url":"https://giphy.com/embed/If1a3H7OHi8vmiCTpW"
"username":"nhl"
"source":""
"title":"Ice Hockey Sport GIF by NHL"
"rating":"g"
"content_url":""
"source_tld":""
"source_post_url":""
"is_sticker":0
"import_datetime":"2019-07-03 17:13:37"
"trending_datetime":"2022-06-04 03:31:37"
"images":{
"original":{
"height":"500"
"width":"500"
"size":"3765578"
"url":"https://media3.giphy.com/media/If1a3H7OHi8vmiCTpW/giphy.gif?cid=84449cd9g1p95f9hyvvtbnajoj9j2ma4tnrpfcurbplr6brq&rid=giphy.gif&ct=g"

我只是想获得该信息URL在原始下方,因此我可以在表视图中显示图像。有人知道为什么我无法访问URL吗?

I am currently trying to create an iOS app that displays trending gifs within a tableview and I have been going crazy trying to access the image URL from the GIPHY API. I am currently using Alamo Fire to access the data and this is how my code looks so far:

import UIKit
import Alamofire

class ViewController: UIViewController {
    
    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        fetchGiphyData()

    }
    
    func fetchGiphyData() {
    
        let request = AF.request("https://api.giphy.com/v1/gifs/trending?api_key=zONZGFIbbvYDBjLEBlgKxLjbsClfmptw&limit=25&rating=pg-13")
        
        request.responseDecodable(of: GiphyData.self) { (response) in
            guard let gifs = response.value else { return }
            print(gifs.height)
            
        }
    }
}

And this is how my data collection is set up for the JSON info:

import UIKit

struct Data: Codable {
    var data: [ImageInfo]
}

struct ImageInfo: Codable {
    var images: ImageTypes
}

struct ImageTypes: Codable {
    var original: GiphyData
}

struct GiphyData: Codable {
    var url: String
    var height: String
}

struct GiphyImages: Codable {
    var giphyImages: [GiphyData]
}

Here is the API info from GIPHY:

{
"data":[
{
"type":"gif"
"id":"If1a3H7OHi8vmiCTpW"
"url":"https://giphy.com/gifs/nhl-draft-nhl-2019-prospects-If1a3H7OHi8vmiCTpW"
"slug":"nhl-draft-nhl-2019-prospects-If1a3H7OHi8vmiCTpW"
"bitly_gif_url":"https://gph.is/g/Zdx6J5o"
"bitly_url":"https://gph.is/g/Zdx6J5o"
"embed_url":"https://giphy.com/embed/If1a3H7OHi8vmiCTpW"
"username":"nhl"
"source":""
"title":"Ice Hockey Sport GIF by NHL"
"rating":"g"
"content_url":""
"source_tld":""
"source_post_url":""
"is_sticker":0
"import_datetime":"2019-07-03 17:13:37"
"trending_datetime":"2022-06-04 03:31:37"
"images":{
"original":{
"height":"500"
"width":"500"
"size":"3765578"
"url":"https://media3.giphy.com/media/If1a3H7OHi8vmiCTpW/giphy.gif?cid=84449cd9g1p95f9hyvvtbnajoj9j2ma4tnrpfcurbplr6brq&rid=giphy.gif&ct=g"

I am just trying to get the URL under original so I can display the image within the table view. Does anyone know why I can't access the URL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文