SwiftJSON parsing { Key: {Key: Value} } case?



这是我的json数据:

{
"whois": {
"queryType": "IPv4",
"orgID": "ORG572"
}
}

我想得到"orgID"。

我尝试这样:

switch response.result {
case .success(let value):
let responseJson: JSON = JSON(value)
let orgID = responseJson["whois"]["orgID"].stringValue
print(orgID)
case .failure(let error):
print(error)
}

日志控制台中的"orgID"为空。 我必须做什么?

尝试使用Codable来解析data,而不是使用 SwiftyJSON 等第三方。

模型:

struct Response: Codable {
let whois: Whois
}
struct Whois: Codable {
let queryType, orgID: String
}

像这样解析JSONdata

do {
let response = try JSONDecoder().decode(Response.self, from: data)
print(response.whois.orgID)
} catch {
print(error)
}

最新更新