从 JSONSerialization 结果设置标签
Set label from JSONSerialization result
我有一个 swift 应用程序 Swift 3
与服务器交互以获取数据。现在我仍然可以成功连接到服务器。问题是,当我想从 JSON
结果中获取特定数据以设置标签文本值时,我总是在控制台中得到值 Optional(x)
并且标签值始终是 nil
.
这是我从服务器接收到的数据格式:
[A: 1,
B: 2,
C: 3]
这就是我得到它的方式:
let task = session.dataTask(with: request) { data, response, error in
do {
guard let data = data else {
throw JSONError.NoData
}
guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary else {
throw JSONError.ConversionFailed
}
self.labelA.text = json[“A”] as? String
} catch let error as JSONError {
print(error.rawValue)
} catch let error as NSError {
print(error.debugDescription)
}
}
task.resume()
编辑:
我也可以获得这些格式:
案例一:
[
{
id: 1,
fieldA: “nameA”,
fieldB: [“textA”, “textB", “textC”, “textD”],
fieldC: “nameC”
}
]
案例二:
{
id: 1,
fieldA: “nameA”,
fieldB: [“textA”, “textB", “textC”, “textD”],
fieldC: “nameC”
}
字段B是String数组
总是运行 UI 主线程上的东西:
if let text = json[“A”] as String {
DispatchQueue.main.async {
self.labelA.text = text
}
}
首先,除非别无选择,否则永远不要在 Swift 中使用 NSDictionary
。使用 Swift 本机类型。
字典中所有结果值的第二个似乎是 Int
而不是 String
。
...
guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String:Int] else {
throw JSONError.ConversionFailed
}
if let jsonA = json["A"] {
self.labelA.text = "\(jsonA)"
}
如果值为 String
...
guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String:String] else {
throw JSONError.ConversionFailed
}
if let jsonA = json["A"] {
self.labelA.text = jsonA
}
Case 1可以用如下代码解析,nil合并运算符??
赋默认值以防case密钥不存在。
guard let jsonArray = try JSONSerialization.jsonObject(with: data, options: []) as? [[String:Any]] else {
throw JSONError.ConversionFailed
}
for json in jsonArray {
let identifier = json["id"] as? Int ?? 0
let fieldA = json["fieldA"] as? String ?? ""
let fieldB = json["fieldB"] as? [String] ?? [String]()
let fieldC = json["fieldC"] as? String ?? ""
}
案例 2 是一个字典 [String:Any]
与案例 1 相同,但没有数组循环。
我有一个 swift 应用程序 Swift 3
与服务器交互以获取数据。现在我仍然可以成功连接到服务器。问题是,当我想从 JSON
结果中获取特定数据以设置标签文本值时,我总是在控制台中得到值 Optional(x)
并且标签值始终是 nil
.
这是我从服务器接收到的数据格式:
[A: 1,
B: 2,
C: 3]
这就是我得到它的方式:
let task = session.dataTask(with: request) { data, response, error in
do {
guard let data = data else {
throw JSONError.NoData
}
guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary else {
throw JSONError.ConversionFailed
}
self.labelA.text = json[“A”] as? String
} catch let error as JSONError {
print(error.rawValue)
} catch let error as NSError {
print(error.debugDescription)
}
}
task.resume()
编辑: 我也可以获得这些格式:
案例一:
[
{
id: 1,
fieldA: “nameA”,
fieldB: [“textA”, “textB", “textC”, “textD”],
fieldC: “nameC”
}
]
案例二:
{
id: 1,
fieldA: “nameA”,
fieldB: [“textA”, “textB", “textC”, “textD”],
fieldC: “nameC”
}
字段B是String数组
总是运行 UI 主线程上的东西:
if let text = json[“A”] as String {
DispatchQueue.main.async {
self.labelA.text = text
}
}
首先,除非别无选择,否则永远不要在 Swift 中使用 NSDictionary
。使用 Swift 本机类型。
字典中所有结果值的第二个似乎是 Int
而不是 String
。
...
guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String:Int] else {
throw JSONError.ConversionFailed
}
if let jsonA = json["A"] {
self.labelA.text = "\(jsonA)"
}
如果值为 String
...
guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String:String] else {
throw JSONError.ConversionFailed
}
if let jsonA = json["A"] {
self.labelA.text = jsonA
}
Case 1可以用如下代码解析,nil合并运算符??
赋默认值以防case密钥不存在。
guard let jsonArray = try JSONSerialization.jsonObject(with: data, options: []) as? [[String:Any]] else {
throw JSONError.ConversionFailed
}
for json in jsonArray {
let identifier = json["id"] as? Int ?? 0
let fieldA = json["fieldA"] as? String ?? ""
let fieldB = json["fieldB"] as? [String] ?? [String]()
let fieldC = json["fieldC"] as? String ?? ""
}
案例 2 是一个字典 [String:Any]
与案例 1 相同,但没有数组循环。