Swift4JSON解码
Swift 4 JSON decoding
我正在尝试解码 JSON。我用于解码 JSON 的 swift 函数是:
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()
}
}
我的 struct
在 Swift 是
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?
}
我的服务器响应是:
{"chap":{"Name":"Why Chapel","Loc":"FEC","Year":2018,"Month":9,"Day":4,"Hour":16,"Min":1,"Sec":7}}
然而,当我 运行 这个程序打印出 "Error: Couldn't decode data into chapData" 并且我不知道为什么。
首先catch
解码错误。从来没有try?
。捕获的错误更具描述性
Expected to decode Array<Any>
but found a dictionary instead
表示:key chap
的值是字典,不是数组
struct Chapel: Decodable {
let chap: Chap
}
然后你必须打印
print(chapData.chap.Day)
您可以减少代码。不需要默认 GET 请求的显式 URLRequest
和 headers。这足够了:
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
...
我正在尝试解码 JSON。我用于解码 JSON 的 swift 函数是:
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()
}
}
我的 struct
在 Swift 是
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?
}
我的服务器响应是:
{"chap":{"Name":"Why Chapel","Loc":"FEC","Year":2018,"Month":9,"Day":4,"Hour":16,"Min":1,"Sec":7}}
然而,当我 运行 这个程序打印出 "Error: Couldn't decode data into chapData" 并且我不知道为什么。
首先catch
解码错误。从来没有try?
。捕获的错误更具描述性
Expected to decode
Array<Any>
but found a dictionary instead
表示:key chap
的值是字典,不是数组
struct Chapel: Decodable {
let chap: Chap
}
然后你必须打印
print(chapData.chap.Day)
您可以减少代码。不需要默认 GET 请求的显式 URLRequest
和 headers。这足够了:
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
...