【发布时间】:2020-02-26 15:25:20
【问题描述】:
我正在调用 api 来获取一些数据,json dataAsString 看起来像这样......
guard let dataAsString = String(data: data, encoding: .utf8)else {return}
print(dataAsString)
JSON 数据
{"patch_report":{"name":"macOS Updates","patch_software_title_id":"1","total_computers":"5","total_versions":"1","versions":{"version":{"software_version":"10.15.3","computers":{"size":"5","computer":{"id":"467","name":"EPART1BGF8J9"}}}}}}
do {
// make sure this JSON is in the format we expect
if let json = try JSONSerialization.jsonObject(with:data, options:[]) as? [String: Any] {
// try to read out a string array
if let patch_report = json["patch_report"] as? [String] {
print(patch_report)
}
}
} catch let error as NSError {
print("Failed to load: \(error.localizedDescription)")
}
如何使用 JSONSerialization 仅获取某些嵌套的 JSON 值? 许多人告诉我不要使用 JSONSerialization,所以我提供了一个替代解决方案。下面也有 Codable 解决方案。 Click Here For An Explination - Using Codable with nested JSON!
我能够在下面找到答案...
使用 JSONSerialization 获取嵌套 JSON
do {
// make sure this JSON is in the format we expect
if let json = try JSONSerialization.jsonObject(with:data, options:[]) as? [String: Any] {
// try to read out a string array
if let patch_report = json["patch_report"] as? [String:Any] {
if let versions = patch_report["versions"] as? [String:Any] {
if let version = versions["version"] as? [String:Any] {
if let software_version = version["software_version"] as? String {
print(software_version)
}
}}
} else {print("Not Available")}
}
} catch let error as NSError {
print("Failed to load: \(error.localizedDescription)")
}
使用解码器获取嵌套的 Json
struct PatchReport: Codable {
var patch_report:Patch_report?
enum CodingKeys: String, CodingKey{
case patch_report = "patch_report"
}
struct Patch_report: Codable {
var name:String?
var patch_software_title_id:String?
var total_computers:String?
var total_versions:String?
var versions: Versions?
enum CodingKeys: String, CodingKey {
case name = "name"
case patch_software_title_id = "patch_software_title_id"
case total_computers = "total_computers"
case total_versions = "total_versions"
case versions = "versions"
}
struct Versions: Codable {
var version: Version?
enum CodingKeys: String, CodingKey {
case version = "version"
}
struct Version: Codable {
var software_version:String?
var computers:Computers?
enum CodingKeys: String, CodingKey {
case software_version = "software_version"
case computers = "computers"
}
struct Computers: Codable{
var size:String?
var computer:Computer?
enum CodingKeys: String, CodingKey {
case size = "size"
case computer = "computer"
}
struct Computer: Codable{
var id:String?
var name:String?
enum CodingKeys: String, CodingKey {
case id = "id"
case name = "name"
}
}
}
}
}
}
}
let response = try! JSONDecoder().decode(PatchReport.self, from: data)
print(String(response.patch_report?.total_computers ?? ""))
【问题讨论】:
-
放弃 JSON 序列化。改用 Codable。无论如何
json["patch_report"]不是[String]。这是一本字典。 -
Codable 结构会是什么样子?
-
if let patch_report = json["patch_report"] as? [String] { A() } else { B() },重要的是你知道什么可以让它去 B() 和什么去 A()。如果json["patch_report"]为零,则转到 B()。如果json["patch_report"]不是[String](字符串数组),它将转到B()。你认为哪个原因适合你?这是调试和自治的重要一步。
标签: json swift string-parsing