JSONDecoder - "Expected to decode Dictionary<String, Any> but found a string/data instead."
JSONDecoder - "Expected to decode Dictionary<String, Any> but found a string/data instead."
所以我正在尝试解码 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: "Expected to decode Dictionary but found a string/data instead.", underlyingError: nil))
发生这种情况是因为 Info
值实际上是一个字符串而不是字典。
请注意它以引号开头。
将模型更改为 return 字典而不是字符串。
这个
"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?
您复制了 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": "x",
"FriendlyName": "DH-390 2MP ENG031",
"UpTime": "548"
}
}
}
然后可以考虑更改服务器输出,如果不能则手动解码 info
按照 this guide,它从 Manual Encoding 和用重要的位解码。
所以我正在尝试解码 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: "Expected to decode Dictionary but found a string/data instead.", underlyingError: nil))
发生这种情况是因为 Info
值实际上是一个字符串而不是字典。
请注意它以引号开头。
将模型更改为 return 字典而不是字符串。
这个
"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?
您复制了 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": "x",
"FriendlyName": "DH-390 2MP ENG031",
"UpTime": "548"
}
}
}
然后可以考虑更改服务器输出,如果不能则手动解码 info
按照 this guide,它从 Manual Encoding 和用重要的位解码。