在Swift 4中使用Decodable和CodingKeys解析JSON

时间:2017-10-18 22:40:18

标签: json swift extension-methods swift4 codable

我正在尝试解析我从URL检索的大型JSON字符串。我用来测试的JSON如下:

let json = """
{
"feed": {
    "title": "Harry Potter",
    "test": "I dont want this value",
    "results": [
        {
        "author": "JK Rowling",
        "artworkURL": "A url",
        "genres": [
            {
                "name": "Fantasy"
            },
            {
                "name": "Scifi"
            }
        ],
            "name": "Goblet of Fire",
            "releaseDate": "2000-07-08"
        },
        {
        "author": "JK Rowling",
        "artworkURL": "A url",
        "genres": [
            {
                "name": "Fantasy"
            },
            {
                "name": "Scifi"
            }
            ],
            "name": "Half Blood Prince",
            "releaseDate": "2009-07-15"
            }
        ]
    }
}
""".data(using: .utf8)!

我有几个数据结构将数据放入:

struct Genre: Decodable {
    let name: String
}

struct Book: Decodable {
    let author: String
    let artworkURL: URL
    let genres: [Genre]
    let name: String
    let releaseDate: String
}

struct BookCollection {
    let title: String
    let books: [Book]

    enum CodingKeys: String, CodingKey {
        case feed
    }

    enum FeedKeys: String, CodingKey {
        case title, results
    }

    enum ResultKeys: String, CodingKey {
        case author, artworkURL, genres, name, releaseDate
    }

    enum GenreKeys: String, CodingKey {
        case name
    }
}

extension BookCollection: Decodable {
    init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)

        let feed = try values.nestedContainer(keyedBy: FeedKeys.self, 
    forKey: .feed)
        self.title = try feed.decode(String.self, forKey: .title)
        self.books = try feed.decode([Track].self, forKey: .results)
    }
}

然后我打印出这样的信息:

do {
    let response = try JSONDecoder().decode(BookCollection.self, from: json)
    for book in response.books {
        print(book.genres)
    }
} catch {
    print(error)
}

成功打印除了流派之外的所有信息。这给了我一系列流派,但我不能这样做     book.genres.name 访问该名称。我必须使用:     book.genres[0] 它只给出了第一个索引的结果。

有没有办法可以在我的BookCollection扩展程序中完善我的JSON解码,然后再使用book.genres.name

谢谢

2 个答案:

答案 0 :(得分:1)

如果您确实需要额外的name属性,可以在新的扩展名中执行此操作:

extension Array where Element == Genre {
    var name: [String] {
        return self.map { $0.name }
    }
}

这会将上述name属性添加到每个 [Genre]值,包括由Book类型定义的值。请确保这是你真正想要的(如果将此扩展名声明为private,而不是在相应的swift文件中可用)。

答案 1 :(得分:0)

为了消除使用许多枚举编码键并手动解码类型的需要,您可以更改数据结构以映射JSON结构格式。注意下面的代码,没有必要嵌套结构,你也可以把它并行。此代码使用您编码的JSON数据进行测试

public struct HarryPotterFeed: Codable {
  public let feed: BookCollection

  public struct BookCollection: Codable {
    public let title: String
    public let books: [Book]

    // map properties books to json's "results"
    enum CodingKeys: String, CodingKey {
      case title  // needs to come along 
      case books = "results"
    }

    public struct Book: Codable {
      public let author, name, artworkURL, releaseDate : String
      public let genres: [Genre]

      public struct Genre: Codable {
        public let name: String
      }
    }
  }
}

// Decode JSON 

do {
  let response = try JSONDecoder().decode(HarryPotterFeed.self, from: json)
  for book in response.feed.books {
    for name in book.genres {
      print(name)
    }
  }
} catch {
  print("PROBLEM DECODING JSON \(error)")
}