正在将 JSON 数组解析为标签
Parsing JSON array to label
我正在尝试解析下面的 JSON(实际数据是所列格式的 20 倍)
{
message = "";
result = (
{
Ask = "4.8e-05";
BaseVolume = "32.61025363";
Bid = "4.695e-05";
Created = "2017-06-06T01:22:35.727";
High = "5.44e-05";
Last = "4.69e-05";
Low = "4.683e-05";
MarketName = "BTC-1ST";
OpenBuyOrders = 293;
OpenSellOrders = 4186;
PrevDay = "4.76e-05";
TimeStamp = "2018-02-20T00:00:31.863";
Volume = "662575.93818332";
},
这是我现在拥有的代码。它成功地将值 "Last" 打印到控制台,但是当我合并 Dispatch.Queue 时,我得到一个 Thread 1: signal SIGBRT
没有将值打印到标签。
let myJson = try JSONSerialization.jsonObject(with: content) as! [String:Any]
if let info = myJson["result"] as! [[String:Any]]?
{
for i in 0..<20 {
if i == 1
{
if let dict = info[i] as? [String:Any]
{
if let price = dict["Last"]
{
print(price)
//DispatchQueue.main.async
//{
// self.label1.text = price as String
//}
}
}
}
非常感谢任何帮助!
JSON 不使用 =
符号或分号。将每个 =
更改为冒号,将每个分号更改为逗号,以便
Ask = "4.8e-05";
BaseVolume = "32.61025363";
Bid = "4.695e-05";
变成
Ask: "4.8e-05",
BaseVolume: "32.61025363",
Bid: "4.695e-05",
很可能您的 self.label1
插座未连接。修复该连接。
您还应该更新获取 "Last"
键值的 if let
,如下所示:
if let price = dict["Last"] as? String{
print(price)
DispatchQueue.main.async {
self.label1.text = price
}
}
您还可以执行一些其他清理工作:
if let myJson = try JSONSerialization.jsonObject(with: content) as? [String:Any] {
if let info = myJson["result"] as? [[String:Any]] {
for (index, dict) in info.enumerated() {
if index == 1 {
if let price = dict["Last"] as? String {
print(price)
DispatchQueue.main.async {
self.label1.text = price
}
} // else no "Last" or not a String
}
}
} // else "result" doesn't contain expected array of dictionary
} // else content isn't a valid JSON dictionary
避免所有这些强制转换。尤其要避免强制转换为可选的。
我正在尝试解析下面的 JSON(实际数据是所列格式的 20 倍)
{
message = "";
result = (
{
Ask = "4.8e-05";
BaseVolume = "32.61025363";
Bid = "4.695e-05";
Created = "2017-06-06T01:22:35.727";
High = "5.44e-05";
Last = "4.69e-05";
Low = "4.683e-05";
MarketName = "BTC-1ST";
OpenBuyOrders = 293;
OpenSellOrders = 4186;
PrevDay = "4.76e-05";
TimeStamp = "2018-02-20T00:00:31.863";
Volume = "662575.93818332";
},
这是我现在拥有的代码。它成功地将值 "Last" 打印到控制台,但是当我合并 Dispatch.Queue 时,我得到一个 Thread 1: signal SIGBRT
没有将值打印到标签。
let myJson = try JSONSerialization.jsonObject(with: content) as! [String:Any]
if let info = myJson["result"] as! [[String:Any]]?
{
for i in 0..<20 {
if i == 1
{
if let dict = info[i] as? [String:Any]
{
if let price = dict["Last"]
{
print(price)
//DispatchQueue.main.async
//{
// self.label1.text = price as String
//}
}
}
}
非常感谢任何帮助!
JSON 不使用 =
符号或分号。将每个 =
更改为冒号,将每个分号更改为逗号,以便
Ask = "4.8e-05";
BaseVolume = "32.61025363";
Bid = "4.695e-05";
变成
Ask: "4.8e-05",
BaseVolume: "32.61025363",
Bid: "4.695e-05",
很可能您的 self.label1
插座未连接。修复该连接。
您还应该更新获取 "Last"
键值的 if let
,如下所示:
if let price = dict["Last"] as? String{
print(price)
DispatchQueue.main.async {
self.label1.text = price
}
}
您还可以执行一些其他清理工作:
if let myJson = try JSONSerialization.jsonObject(with: content) as? [String:Any] {
if let info = myJson["result"] as? [[String:Any]] {
for (index, dict) in info.enumerated() {
if index == 1 {
if let price = dict["Last"] as? String {
print(price)
DispatchQueue.main.async {
self.label1.text = price
}
} // else no "Last" or not a String
}
}
} // else "result" doesn't contain expected array of dictionary
} // else content isn't a valid JSON dictionary
避免所有这些强制转换。尤其要避免强制转换为可选的。