在Swift4中解析JSON



你好,我尝试解码JSON:

{"结果":[ {" id":" 80", "时间":" 09:00:00", "地位":[ {"状态":0," kirpeja_id":" 74","名称":" natalja"," image":" natalija255.png","持续时间":" 00:20:00"}, {"状态":1," Kirpeja_id":" 80","名称":" Lina"," image":" Kazkas.png"," distration":" 00:30:00"}, {"状态":0," kirpeja_id":" 82","名称":" rasa"," image":" img_20170906_171553.jpg","持续时间":00:40:00" 00:40:00"} ]}, {" id":" 81", "时间":" 09:10:00", "地位":[ {"状态":0," Kirpeja_id":" 66","名称":" Ilona"," Image":" Ilona_new.jpg"," distration":" 00:30:00"}, {"状态":0," kirpeja_id":" 74","名称":" natalja"," image":" natalija255.png","持续时间":" 00:20:00"}, {"状态":0," kirpeja_id":" 80","名称":" lina"," image":" kazkas.png"," distration":" 00:30:00"}, {"状态":0," kirpeja_id":" 82","名称":" rasa"," image":" img_20170906_171553.jpg","持续时间":00:40:00" 00:40:00"} ]}, ...

在这里我的课

class TimeStatusResult: Codable {
let result: [TimeStatus]
init (result:[TimeStatus]) {
self.result = result
}
}
class TimeStatus: Codable {
let ID:String?
let time: String?
let status: [Status]
init (status:[Status]) {
    self.ID = ""
    self.time = ""
    self.status = status
}
}
class Status: Codable {
let status: String?
let kirpeja_id: String?
let name: String?
let image: String?
let duration: String?
init () {
    self.status = ""
    self.kirpeja_id = ""
    self.name = ""
    self.image = "nophoto.jpg"
    self.duration = ""
}
}

在这里我的JSON功能

final let jsonUrl = URL(string: "http://**********/getlaikas_new.php")
private var timeStatusResult = [TimeStatus]()
 func downloadJson () {
    guard let downloadURL = jsonUrl else {return}
    var request = URLRequest(url: downloadURL)
    request.setValue("application/x-www-form-urlencoded",forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
    let postString = "data=(pInfo.paslaugosData!)&salonId=(pInfo.ID!)&paslaugos_id=(pInfo.paslaugosId!)"
    request.httpBody = postString.data(using: .utf8, allowLossyConversion: true)
    URLSession.shared.dataTask(with: request) {data, urlResponse, error in
        guard let data = data , error == nil, urlResponse != nil else {
            print ("something wrong")
            return }
        print ("downloaded!")
        do
        {
            let decoder = JSONDecoder()
            print (data)
            let downloadedTimeStatus = try decoder.decode(TimeStatusResult.self, from: data)
            self.timeStatusResult = downloadedTimeStatus.result
            DispatchQueue.main.async {
               // self.kirpejosPaslaugosTable.reloadData()
            }
        } catch {
            print ("something wrong after download")
        }
        }.resume()
}

在这条线我有问题

let downloadedTimeStatus = try decoder.decode(TimeStatusResult.self, from: data)

有人可以帮助我吗?:(

错误非常明显:

... burzua_1。状态。(codingkeys in _479abd1af7892c92c9f2f2fd23ec23214e088(。 status>状态],debugDescription,debugDescription:" debugdescription:" trield the thind thig the thule /strong>"

status的值是不是在双引号中,因此是Int

class Status: Codable {
   let status: Int
   ...

请不要粗心地将所有属性声明为可选。例如,所有Status字典中都存在status以及所有其他键。

相关内容

  • 没有找到相关文章

最新更新