简体   繁体   中英

Swift 4 JSON decoding

I am trying to decode JSON. My swift function for decoding the JSON is:

func GetChapInfo(){

    let endpoint = "https://chapel-logs.herokuapp.com/chapel"

    let endpointUrl = URL(string: endpoint)

    do {
        var request = URLRequest(url: endpointUrl!)
        request.httpMethod = "GET"
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        request.addValue("application/json", forHTTPHeaderField: "Accept")

        let task = URLSession.shared.dataTask(with: request){
            (data: Data?, response: URLResponse?, error: Error?) in


            let dataAsString = String(data: data!, encoding: .utf8)
            //print(dataAsString)

            if(error != nil) {
                print("Error")
            }
            else{
                do{
                    guard let chapData = try? JSONDecoder().decode(Chapel.self, from: data!) else {
                        print("Error: Couldn't decode data into chapData")
                        return
                    }
                    for E in chapData.chap {
                        print(E.Day as Any)
                    }
                }
        }
        }

        task.resume()
    }
}

my struct in Swift is

struct Chapel: Decodable {
    let chap: [Chap]
}

struct Chap: Decodable {
    let Name: String?
    let Loc: String?
    let Year: Int?
    let Month: Int?
    let Day: Int?
    let Hour: Int?
    let Min: Int?
    let Sec: Int?
}

and my response from the server is:

{"chap":{"Name":"Why Chapel","Loc":"FEC","Year":2018,"Month":9,"Day":4,"Hour":16,"Min":1,"Sec":7}}

However when I run this the program prints out "Error: Couldn't decode data into chapData" and I cannot figure out why.

First of all catch decoding errors. Never try? . The caught error is much more descriptive

Expected to decode Array<Any> but found a dictionary instead

Means: The value for key chap is a dictionary, not an array

struct Chapel: Decodable {
    let chap: Chap
}

And then you have to print

print(chapData.chap.Day) 

You can reduce your code. An explicit URLRequest and headers for a default GET request is not needed. This is sufficient:

let endpoint = "https://chapel-logs.herokuapp.com/chapel"
let endpointUrl = URL(string: endpoint)!

do {

    let task = URLSession.shared.dataTask(with: endpointUrl) { (data, response, error) in
    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM