Json 我能';不要打开图像。解析不';行不通

Json 我能';不要打开图像。解析不';行不通,json,swift,xcode,Json,Swift,Xcode,我无法在“图像”中打开“url”。 解析不起作用。请帮忙。多谢各位 "images": [ { "url": "https://ktar.com/wp-content/uploads/2020/03/ap_27df5153e3bf48198ebcfd40900446d6.jpg", "width": 1280, "height": 853, "title": "Arizona reports 124

我无法在“图像”中打开“url”。 解析不起作用。请帮忙。多谢各位

"images": [
        {
          "url": "https://ktar.com/wp-content/uploads/2020/03/ap_27df5153e3bf48198ebcfd40900446d6.jpg",
          "width": 1280,
          "height": 853,
          "title": "Arizona reports 124 new coronavirus cases, five additional deaths",
          "attribution": null
======================================================= ======================================================= 原料药

====================================================== ======================================================

//  SPORTNEWSVC.swift
//  Hero

import Alamofire
import Kingfisher
import UIKit


class NewsVC: UICollectionViewController, UICollectionViewDelegateFlowLayout {

    var news = [Article]()

    override func viewDidLoad() {
        super.viewDidLoad()

        getArticles()

    }

    func getArticles() {

        let parameters: Parameters = ["Subscription-Key": "3009d4ccc29e4808af1ccc25c69b4d5d"]

        Alamofire.request("https://api.smartable.ai/coronavirus/news/US", parameters: parameters).responseData { (response) in

            guard let data = response.data else { return }

            do {

//                let json = try JSONSerialization.jsonObject(with: data, options: [])
//                print(json)

                let topHeadlinesResponse = try JSONDecoder().decode(TopHeadlinesResponse.self, from: data)
                self.news = topHeadlinesResponse.news
                self.collectionView?.reloadData()

            } catch {
                print(error)
            }

        }

    }


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return news.count
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ArticleCell", for: indexPath) as? ArticleCell else { return UICollectionViewCell ()}

        let article = news[indexPath.item]
        cell.populate(with: article)

        return cell

    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        let height: CGFloat = 277

        let width = (collectionView.frame.width / 2) - 2

        let size = CGSize(width: width, height: height)

        return size

    }

}
//  Article.swift
//  Hero
//

import Foundation

struct Article: Decodable {
    let headline: String
    let url: String?

    private enum CodingKeys: String, CodingKey {
        case headline = "title"
        case url
    }

}
//
//  ArticleCell.swift
//  Hero

import UIKit
import Kingfisher

class ArticleCell: UICollectionViewCell {

    @IBOutlet weak var articleImageView: UIImageView!
    @IBOutlet weak var headlieLabel: UILabel!

    func populate(with article: Article){
        headlieLabel.text = article.headline

        if let url = article.url {
            let url = URL(string: url)
            articleImageView.kf.setImage(with: url)
        }

    }

}
import Foundation


struct TopHeadlinesResponse: Decodable {
    let news: [Article]
}
====================================================== ======================================================

//  SPORTNEWSVC.swift
//  Hero

import Alamofire
import Kingfisher
import UIKit


class NewsVC: UICollectionViewController, UICollectionViewDelegateFlowLayout {

    var news = [Article]()

    override func viewDidLoad() {
        super.viewDidLoad()

        getArticles()

    }

    func getArticles() {

        let parameters: Parameters = ["Subscription-Key": "3009d4ccc29e4808af1ccc25c69b4d5d"]

        Alamofire.request("https://api.smartable.ai/coronavirus/news/US", parameters: parameters).responseData { (response) in

            guard let data = response.data else { return }

            do {

//                let json = try JSONSerialization.jsonObject(with: data, options: [])
//                print(json)

                let topHeadlinesResponse = try JSONDecoder().decode(TopHeadlinesResponse.self, from: data)
                self.news = topHeadlinesResponse.news
                self.collectionView?.reloadData()

            } catch {
                print(error)
            }

        }

    }


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return news.count
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ArticleCell", for: indexPath) as? ArticleCell else { return UICollectionViewCell ()}

        let article = news[indexPath.item]
        cell.populate(with: article)

        return cell

    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        let height: CGFloat = 277

        let width = (collectionView.frame.width / 2) - 2

        let size = CGSize(width: width, height: height)

        return size

    }

}
//  Article.swift
//  Hero
//

import Foundation

struct Article: Decodable {
    let headline: String
    let url: String?

    private enum CodingKeys: String, CodingKey {
        case headline = "title"
        case url
    }

}
//
//  ArticleCell.swift
//  Hero

import UIKit
import Kingfisher

class ArticleCell: UICollectionViewCell {

    @IBOutlet weak var articleImageView: UIImageView!
    @IBOutlet weak var headlieLabel: UILabel!

    func populate(with article: Article){
        headlieLabel.text = article.headline

        if let url = article.url {
            let url = URL(string: url)
            articleImageView.kf.setImage(with: url)
        }

    }

}
import Foundation


struct TopHeadlinesResponse: Decodable {
    let news: [Article]
}
====================================================== ======================================================

//  SPORTNEWSVC.swift
//  Hero

import Alamofire
import Kingfisher
import UIKit


class NewsVC: UICollectionViewController, UICollectionViewDelegateFlowLayout {

    var news = [Article]()

    override func viewDidLoad() {
        super.viewDidLoad()

        getArticles()

    }

    func getArticles() {

        let parameters: Parameters = ["Subscription-Key": "3009d4ccc29e4808af1ccc25c69b4d5d"]

        Alamofire.request("https://api.smartable.ai/coronavirus/news/US", parameters: parameters).responseData { (response) in

            guard let data = response.data else { return }

            do {

//                let json = try JSONSerialization.jsonObject(with: data, options: [])
//                print(json)

                let topHeadlinesResponse = try JSONDecoder().decode(TopHeadlinesResponse.self, from: data)
                self.news = topHeadlinesResponse.news
                self.collectionView?.reloadData()

            } catch {
                print(error)
            }

        }

    }


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return news.count
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ArticleCell", for: indexPath) as? ArticleCell else { return UICollectionViewCell ()}

        let article = news[indexPath.item]
        cell.populate(with: article)

        return cell

    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        let height: CGFloat = 277

        let width = (collectionView.frame.width / 2) - 2

        let size = CGSize(width: width, height: height)

        return size

    }

}
//  Article.swift
//  Hero
//

import Foundation

struct Article: Decodable {
    let headline: String
    let url: String?

    private enum CodingKeys: String, CodingKey {
        case headline = "title"
        case url
    }

}
//
//  ArticleCell.swift
//  Hero

import UIKit
import Kingfisher

class ArticleCell: UICollectionViewCell {

    @IBOutlet weak var articleImageView: UIImageView!
    @IBOutlet weak var headlieLabel: UILabel!

    func populate(with article: Article){
        headlieLabel.text = article.headline

        if let url = article.url {
            let url = URL(string: url)
            articleImageView.kf.setImage(with: url)
        }

    }

}
import Foundation


struct TopHeadlinesResponse: Decodable {
    let news: [Article]
}
====================================================== ======================================================

//  SPORTNEWSVC.swift
//  Hero

import Alamofire
import Kingfisher
import UIKit


class NewsVC: UICollectionViewController, UICollectionViewDelegateFlowLayout {

    var news = [Article]()

    override func viewDidLoad() {
        super.viewDidLoad()

        getArticles()

    }

    func getArticles() {

        let parameters: Parameters = ["Subscription-Key": "3009d4ccc29e4808af1ccc25c69b4d5d"]

        Alamofire.request("https://api.smartable.ai/coronavirus/news/US", parameters: parameters).responseData { (response) in

            guard let data = response.data else { return }

            do {

//                let json = try JSONSerialization.jsonObject(with: data, options: [])
//                print(json)

                let topHeadlinesResponse = try JSONDecoder().decode(TopHeadlinesResponse.self, from: data)
                self.news = topHeadlinesResponse.news
                self.collectionView?.reloadData()

            } catch {
                print(error)
            }

        }

    }


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return news.count
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ArticleCell", for: indexPath) as? ArticleCell else { return UICollectionViewCell ()}

        let article = news[indexPath.item]
        cell.populate(with: article)

        return cell

    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        let height: CGFloat = 277

        let width = (collectionView.frame.width / 2) - 2

        let size = CGSize(width: width, height: height)

        return size

    }

}
//  Article.swift
//  Hero
//

import Foundation

struct Article: Decodable {
    let headline: String
    let url: String?

    private enum CodingKeys: String, CodingKey {
        case headline = "title"
        case url
    }

}
//
//  ArticleCell.swift
//  Hero

import UIKit
import Kingfisher

class ArticleCell: UICollectionViewCell {

    @IBOutlet weak var articleImageView: UIImageView!
    @IBOutlet weak var headlieLabel: UILabel!

    func populate(with article: Article){
        headlieLabel.text = article.headline

        if let url = article.url {
            let url = URL(string: url)
            articleImageView.kf.setImage(with: url)
        }

    }

}
import Foundation


struct TopHeadlinesResponse: Decodable {
    let news: [Article]
}
其思想是,JSON响应中的“图像”是一个数组,您需要在可编码结构中反映这一事实。解码魔术需要您遵循JSON数据结构和命名:因此,如果您在“image”字典中遇到字段“width”,您需要在“image”字段中有一个名为“width”的字段(例如,将image作为单独的结构)

您可以阅读更多关于可分解的内容


JSON的顶层没有键
news
“解析不起作用”?你怎么知道的<代码>打印(错误)这叫什么?谢谢。我还在想out@DayMan尝试使用SDweb库,我在扩展名中键入了代码。