简体   繁体   中英

parsing Json in swift4

hello i try to decode Json:

{"result":[ {"ID":"80", "time":"09:00:00", "status":[ {"status":0,"kirpeja_id":"74","name":"Natalja","image":"natalija255.png","duration":"00:20:00"}, {"status":1,"kirpeja_id":"80","name":"Lina","image":"kazkas.png","duration":"00:30:00"}, {"status":0,"kirpeja_id":"82","name":"Rasa ","image":"IMG_20170906_171553.jpg","duration":"00:40:00"} ]}, {"ID":"81", "time":"09:10:00", "status":[ {"status":0,"kirpeja_id":"66","name":"Ilona","image":"ilona_new.jpg","duration":"00:30:00"}, {"status":0,"kirpeja_id":"74","name":"Natalja","image":"natalija255.png","duration":"00:20:00"}, {"status":0,"kirpeja_id":"80","name":"Lina","image":"kazkas.png","duration":"00:30:00"}, {"status":0,"kirpeja_id":"82","name":"Rasa ","image":"IMG_20170906_171553.jpg","duration":"00:40:00"} ]}, ...

here my classes

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 = ""
}
}

here my json function

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()
}

in this line i have issue

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

somebody can help me? :(

The error is very clear:

...burzua_1. Status.(CodingKeys in _479ABD1AF7892C9F2FD23EC23214E088). status ], debugDescription: " Expected to decode String but found a number instead. "

The value for key status is not in double quotes so it's an Int

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

Please don't declare all properties carelessly as optional. For example status as well as all other keys is present in all Status dictionaries.

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