【发布时间】:2018-01-30 14:42:08
【问题描述】:
后端返回位置的自定义 JSON 值。如示例所示:
{
"location": (54.000000, 21.000000)
}
为了解析 JSON,我使用以下代码:
let json = """
{
"location": (54.000000, 21.000000)
}
"""
struct Location: Codable {
var latitude: Double
var longitude: Double
}
let dataJson = json.data(using: .utf8)!
let location = try? JSONDecoder().decode(Location.self, from: dataJson)
当我尝试使用 JSONDecoder 创建 Location 对象时,它给了我一个错误:给定的数据不是有效的 JSON。
dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 18." UserInfo={NSDebugDescription=Invalid value around character 18.})))
我知道它不是有效的 JSON。 覆盖哪些方法可以解析无效的 JSON 值?
【问题讨论】:
-
你不能改变你的后端来返回一个有效的 JSON 吗?
-
@Balanced Backend 由 3rd 方管理,无法进行更改。
标签: swift4 jsondecoder