如何使用 URLSession 从 HTTP 请求的 JSON 对象中获取特定属性
How to get specific properties from a JSON object from HTTP request using URLSession
通过以下代码,我能够有效地获取 JSON 对象,但我不确定如何从对象中检索特定属性。
Swift代码
@IBAction func testing(_ sender: Any) {
let url = URL(string: "http://example.com/cars/mustang")
let task = URLSession.shared.dataTask(with: url!) { data, response, error in
guard error == nil else {
print(error!)
return
}
guard let data = data else {
print("Data is empty")
return
}
let json = try! JSONSerialization.jsonObject(with: data, options: [])
print(json)
}
task.resume()
}
这是我在 运行 上面的代码时看到的...
输出 - JSON 对象
(
{
color = "red";
engine = "5.0";
}
)
我怎样才能得到 属性 颜色?
谢谢
创建一个确认可解码协议的class;例如您的 CarInfo
class CarInfo: Decodable
创建 class
的属性
var color: String
var engine: String
创建 JSON 继承自 CodingKey
的密钥枚举
enum CarInfoCodingKey: String, CodingKey {
case color
case engine
}
执行初始化
required init(from decoder: Decoder) throws
class 将是
class CarInfo: Decodable {
var color: String
var engine: String
enum CarInfoCodingKey: String, CodingKey {
case color
case engine
}
public init(from decoder: Decodabler) throws {
let container = try decoder.container(keyedBy: CarInfoCodingKey.self)
self.color = try container.decode(String.self, forKey: .color)
self.engine = try contaire.decode(String.self, forKey: .engine)
}
}
调用解码器
let carinfo = try JsonDecoder().decode(CarInfo.self, from: jsonData)
这是我的做法...
@IBAction func testing(_ sender: Any) {
let url = URL(string: "http://example.com/cars/mustang")
let task = URLSession.shared.dataTask(with: url!) { data, response, error in
guard error == nil else {
print(error!)
return
}
guard let data = data else {
print("Data is empty")
return
}
let json = try! JSONSerialization.jsonObject(with: data, options: [])
guard let jsonArray = json as? [[String: String]] else {
return
}
let mustangColor = jsonArray[0]["color"]
print(mustangColor!)
}
task.resume()
}
通过以下代码,我能够有效地获取 JSON 对象,但我不确定如何从对象中检索特定属性。
Swift代码
@IBAction func testing(_ sender: Any) {
let url = URL(string: "http://example.com/cars/mustang")
let task = URLSession.shared.dataTask(with: url!) { data, response, error in
guard error == nil else {
print(error!)
return
}
guard let data = data else {
print("Data is empty")
return
}
let json = try! JSONSerialization.jsonObject(with: data, options: [])
print(json)
}
task.resume()
}
这是我在 运行 上面的代码时看到的...
输出 - JSON 对象
(
{
color = "red";
engine = "5.0";
}
)
我怎样才能得到 属性 颜色?
谢谢
创建一个确认可解码协议的class;例如您的 CarInfo
class CarInfo: Decodable
创建 class
的属性var color: String
var engine: String
创建 JSON 继承自 CodingKey
的密钥枚举enum CarInfoCodingKey: String, CodingKey {
case color
case engine
}
执行初始化
required init(from decoder: Decoder) throws
class 将是
class CarInfo: Decodable {
var color: String
var engine: String
enum CarInfoCodingKey: String, CodingKey {
case color
case engine
}
public init(from decoder: Decodabler) throws {
let container = try decoder.container(keyedBy: CarInfoCodingKey.self)
self.color = try container.decode(String.self, forKey: .color)
self.engine = try contaire.decode(String.self, forKey: .engine)
}
}
调用解码器
let carinfo = try JsonDecoder().decode(CarInfo.self, from: jsonData)
这是我的做法...
@IBAction func testing(_ sender: Any) {
let url = URL(string: "http://example.com/cars/mustang")
let task = URLSession.shared.dataTask(with: url!) { data, response, error in
guard error == nil else {
print(error!)
return
}
guard let data = data else {
print("Data is empty")
return
}
let json = try! JSONSerialization.jsonObject(with: data, options: [])
guard let jsonArray = json as? [[String: String]] else {
return
}
let mustangColor = jsonArray[0]["color"]
print(mustangColor!)
}
task.resume()
}