Swift:解析数据可编码协议不起作用

时间:2018-06-26 08:31:57

标签: json swift

我有一个返回json文件的链接,我尝试打印数据,但它总是无效,但它不起作用,这是链接: http://heroapps.co.il/employee-tests/ios/logan.json

我的代码:

    struct DataClass: Codable {
    let name: String?
    let nickname: String?
    let image: URL?
    let dateOfBirth: Int?
    let powers: [String]?
    let actorName: String?
    let movies: [Movie]?

    enum CodingKeys: String, CodingKey {
        case name = "name"
        case nickname = "nickname"
        case image = "image"
        case dateOfBirth = "dateOfBirth"
        case powers = "powers"
        case actorName = "actorName"
        case movies = "movies"
    }
}

struct Movie: Codable {
    let name: String?
    let year: Int?

    enum CodingKeys: String, CodingKey {
        case name = "name"
        case year = "year"
    }
}
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()


        guard let gitUrl = URL(string: "http://heroapps.co.il/employee-tests/ios/logan.json") else { return }
        URLSession.shared.dataTask(with: gitUrl) { (data, response, error) in
            guard let data = data else { return }

                do {
                    let decoder = JSONDecoder()
                    let gitData = try decoder.decode(Movie.self, from: data)
                    print(gitData.name ?? "") //Print nil

                } catch let err {
                    print("Err", err)
                }

            }.resume()

    }

感谢您帮助我找到错误的出处,这是我第一次使用此方法来检索JSON数据

1 个答案:

答案 0 :(得分:3)

您没有解析JSON的顶层。 (successerrorCodemessagedata)。

用于测试的游乐场代码...

import Foundation

let jsonData = """
{
  "success": true,
  "errorCode": 0,
  "message": "Succcess",
  "data": {
    "name": "Logan Howlett",
    "nickname": "The Wolverine",
    "image": "http://heroapps.co.il/employee-tests/ios/logan.jpg",
    "dateOfBirth": 1880,
    "powers": [
      "Adamantium Bones",
      "Self-Healing",
      "Adamantium Claws"
    ],
    "actorName": "Hugh Jackman",
    "movies": [
      {
        "name": "X-Men Origins: Wolverine",
        "year": 2009
      },
      {
        "name": "The Wolverine",
        "year": 2013
      },
      {
        "name": "X-Men: Days of Future Past",
        "year": 2014
      },
      {
        "name": "Logan",
        "year": 2017
      }
    ]
  }
}
""".data(using: .utf8)!

struct JSONResponse: Codable {
    let success: Bool
    let errorCode: Int
    let message: String
    let data: DataClass
}

struct DataClass: Codable {
    let name: String?
    let nickname: String?
    let image: URL?
    let dateOfBirth: Int?
    let powers: [String]?
    let actorName: String?
    let movies: [Movie]?

    enum CodingKeys: String, CodingKey {
        case name = "name"
        case nickname = "nickname"
        case image = "image"
        case dateOfBirth = "dateOfBirth"
        case powers = "powers"
        case actorName = "actorName"
        case movies = "movies"
    }
}

struct Movie: Codable {
    let name: String?
    let year: Int?

    enum CodingKeys: String, CodingKey {
        case name = "name"
        case year = "year"
    }
}

do {
    let result = try JSONDecoder().decode(JSONResponse.self, from: jsonData)
    print(result)
} catch {
    print(error)
}