简体   繁体   English

Swift Json 响应解码

[英]Swift Json Response Decoding

Trying to decode a json response from an https call.尝试解码来自 https 调用的 json 响应。 Code that is doing the decoding:进行解码的代码:

if let data = responseData, let _ = String(data: data, encoding: .utf8) {
        if let httpResponse = response as? HTTPURLResponse{
            if httpResponse.statusCode == 401 {
                print("Not Authorized")
            } else if httpResponse.statusCode == 200 {
                let decoder = JSONDecoder()
                let model: [ListResponse] = try! decoder.decode([ListResponse].self, from: data)
                print("Model: \(model)")

            }
        }
    }

It just keeps outputting an empty array.它只是不断输出一个空数组。 I'm obviously missing something can anyone help?我显然错过了任何人都可以帮忙的东西吗? I can call the method of the api from PostMan with the same information that I'm passing from Swift and it returns my values.我可以使用从 Swift 传递的相同信息从 PostMan 调用 api 的方法,并返回我的值。 For some reason the parsing of the return json is failing with no errors.由于某种原因,返回 json 的解析失败且没有错误。

Edit: Response Data:编辑:响应数据:

[
    {
        "id": 1,
        "numb": "12345",
        "bName": "Test Tester",
        "clDate": "2018-12-31T00:00:00",
        "currSt": "OK",
        "proPerc": 10,
        "prop": "TBD"
    },
    {
        "id": 2,
        "numb": "123456",
        "bName": "Test Tester2",
        "clDate": "2018-12-31T00:00:00",
        "currSt": "OK",
        "proPerc": 20,
        "prop": "TBD"
    }
]

Comes down to be an issue with parsing the clDate from above.归结为从上面解析 clDate 的问题。 Only found that error out once I converted the json to a string and tried to parse that.只有在我将 json 转换为字符串并尝试解析它后才发现该错误。 Trying to figure out how to handle date json parsing now.现在试图弄清楚如何处理日期 json 解析。

Put the below in a playground.把下面的放在操场上。 The next time you have to do this sort of thing, remember Playgrounds are your friend:下次您必须执行此类操作时,请记住 Playgrounds 是您的朋友:

struct ListResponse: Decodable {
    let id: Int
    let numb: String
    let bName: String
    let clDate: Date
    let currSt: String
    let proPerc: Int
    let prop: String
}

let myDateFormatter: DateFormatter = {
    let formatter = DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
    formatter.calendar = Calendar(identifier: .iso8601)
    formatter.timeZone = TimeZone(secondsFromGMT: 0)
    return formatter
}()

let text =
"""
[
{
"id": 1,
"numb": "12345",
"bName": "Test Tester",
"clDate": "2018-12-31T00:00:00",
"currSt": "OK",
"proPerc": 10,
"prop": "TBD"
},
{
"id": 2,
"numb": "123456",
"bName": "Test Tester2",
"clDate": "2018-12-31T00:00:00",
"currSt": "OK",
"proPerc": 20,
"prop": "TBD"
}
]
"""
let data = text.data(using: .utf8)!
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .formatted(myDateFormatter)
let model: [ListResponse] = try! decoder.decode([ListResponse].self, from: data)
print("Model: \(model)")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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