0

所以我试图解码一个json并得到这个错误。

这是 JSON:

{ "SERVERWebSystemInfoGet": { 
        "Return Code" : 0,
        "Return String" : "No Error",
        "Info" : "{\"IT\":\"IT109200310_0\",\"MAC\":\"00:40:7F:41:F8:81\",\"UUID\":\"uuid:858fba00-d3a0-11dd-a001-00407f41f881\",\"SN\":\"ENG031\",\"ModelNumber\":\"DH-390 2MP\",\"ModelName\":\"DH-390 2MP\",\"FwVer\":\"v1.0.0.34\",\"HwVer\":\"\",\"FriendlyName\":\"DH-390 2MP ENG031\",\"UpTime\":548}" }
}

这是我的模型:

struct Information: Codable {

    let ModelName : String?

}

struct GetInformation: Codable {

    let Info: [String: Information]?

}

struct WebSystemInfo: Codable {

    let SERVERWebSystemInfoGet: GetInformation?

}

这是方法:

func parseGetInfo(data: Data) {

    do {
        let info = try JSONDecoder().decode(WebSystemInfo.self, from: data)
        print(info)
    } catch let error{
        print(error)
    }
}

这是我得到的错误:

typeMismatch(Swift.Dictionary Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "SERVERWebSystemInfoGet", intValue: nil), CodingKeys(stringValue: "Info", intValue: nil)], debugDescription: "预期解码 Dictionary 但发现一个字符串/数据。”,基础错误:无))

4

3 回答 3

3

 "Info" : "{\"IT\":\"IT109200310_0\",\"MAC\":\"00:40:7F:41:F8:81\",\"UUID\":\"uuid:858fba00-d3a0-11dd-a001-00407f41f881\",\"SN\":\"ENG031\",\"ModelNumber\":\"DH-390 2MP\",\"ModelName\":\"DH-390 2MP\",\"FwVer\":\"v1.0.0.34\",\"HwVer\":\"\",\"FriendlyName\":\"DH-390 2MP ENG031\",\"UpTime\":548}" }

是一个 json 字符串而不是你需要的字典

let Info:String?
于 2018-08-27T11:55:04.483 回答
2

发生这种情况是因为该Info值实际上是一个字符串而不是字典。请注意,它以引号开头。

更改模型以返回 Dictionary 而不是 String。

于 2018-08-27T11:53:56.867 回答
1
于 2018-08-27T12:04:16.340 回答