-2

I am getting an error when decoding JSON in swift 4.2

Expected to decode Array but found a dictionary instead.

My JSON Model:

public struct NewsSource: Equatable, Decodable {

public let id: String?
public let name: String?
public let sourceDescription: String?
public let url: URL?

enum CodingKeys: String, CodingKey {
    case id
    case name
    case sourceDescription = "description"
    case url

}

public init(id: String,
            name: String,
            sourceDescription: String,
            url: URL,
            category: NewsCategory,
            language: NewsLanguage,
            country: NewsCountry) {
    self.id = id
    self.name = name
    self.sourceDescription = sourceDescription
    self.url = url
} }

How I fetch the JSON:

func fetchJSON() {

let urlString = "https://newsapi.org/v2/sources?apiKey=myAPIKey"

guard let url = URL(string: urlString) else { return }
    URLSession.shared.dataTask(with: url) { (data, _, err) in
        DispatchQueue.main.async {
            if let err = err {
                print("Failed to get data from url:", err)
                return
            }

            guard let data = data else { return }
            print(data)
            do {

                let decoder = JSONDecoder()
                decoder.keyDecodingStrategy = .convertFromSnakeCase

                self.Sources = try decoder.decode([NewsSource].self, from: data)
                self.tableView.reloadData()

            } catch let jsonErr {
                print("Failed to decode:", jsonErr)
            }
        }
        }.resume()
}
Simply Ged
  • 8,250
  • 11
  • 32
  • 40

2 Answers2

2

If you look at the JSON that is being returned it looks like this:

{
    "status": "ok",
    "sources": [{
        "id": "abc-news",
        "name": "ABC News",
        "description": "Your trusted source for breaking news, analysis, exclusive interviews, headlines, and videos at ABCNews.com.",
        "url": "https://abcnews.go.com",
        "category": "general",
        "language": "en",
        "country": "us"
    }, {
        "id": "abc-news-au",
        "name": "ABC News (AU)",
        "description": "Australia's most trusted source of local, national and world news. Comprehensive, independent, in-depth analysis, the latest business, sport, weather and more.",
        "url": "http://www.abc.net.au/news",
        "category": "general",
        "language": "en",
        "country": "au"
    }, 
    ...

While there is an array of sources, the array is not the root. The root of the JSON is an object with a status string and and a sources array. This is why the decoder is failing.

You need to define an additional struct to handle this:

struct NewsResult {
    let status: String
    let sources: [NewsSource]
}

Then you decode this object:

let sourceResult = try decoder.decode(NewsResult.self, from: data)
self.sources = sourceResult.sources
Paulw11
  • 108,386
  • 14
  • 159
  • 186
0

This should be your structure:

struct NewsSource: Codable {
    let status: String
    let sources: [NewsSource]
}

public struct NewsSource: Equatable, Decodable {

public let id: String?
public let name: String?
public let sourceDescription: String?
public let url: URL?

enum CodingKeys: String, CodingKey {
    case id
    case name
    case sourceDescription = "description"
    case url

}

public init(id: String,
            name: String,
            sourceDescription: String,
            url: URL,
            category: NewsCategory,
            language: NewsLanguage,
            country: NewsCountry) {
    self.id = id
    self.name = name
    self.sourceDescription = sourceDescription
    self.url = url
} }

struct Source: Codable {
    let id, name, description: String
    let url: String
    let category: Category
    let language, country: String
}

enum Category: String, Codable {
    case business = "business"
    case entertainment = "entertainment"
    case general = "general"
    case health = "health"
    case science = "science"
    case sports = "sports"
    case technology = "technology"
}

And then to decode it:

let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let newsSource = try? decoder.decode(NewsSource.self, from: data)
self.Sources = newsSource.sources
self.tableView.reloadData()

Hope this helps!

Oscar
  • 404
  • 7
  • 19