API返回下面的響應,但我不知道如何解析它。如何從複雜的字典中獲取價值?
響應JSON
[
"results": [
[
"address": "mgG2W14th6TXYWXNDrZ24shsJ2wYhJm2b3",
"total": [
"balance": 0,
"received": 0,
"sent": 0
],
"confirmed": [
"balance": 0,
"received": 0,
"sent": 0
]
]
]
]
Swift代碼
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.chain!.getAddress(address!) { dictionary, error in
NSLog("%@", dictionary)
}
Dictionary對象
(lldb) po dictionary
[results: (
{
address = mgG2W14th6TXYWXNDrZ24shsJ2wYhJm2b3;
confirmed = {
balance = 0;
received = 0;
sent = 0;
};
total = {
balance = 0;
received = 0;
sent = 0;
};
}
)]
我嘗試了很多次......你能不能分享一下如何解決這個問題,請..
(lldb) po dictionary["results"]![0]
{
address = mgG2W14th6TXYWXNDrZ24shsJ2wYhJm2b3;
confirmed = {
balance = 0;
received = 0;
sent = 0;
};
total = {
balance = 0;
received = 0;
sent = 0;
};
}
po dictionary["results"]![0]!["address"]
error: <EXPR>:1:28: error: cannot subscript a value of type 'AnyObject' with an index of type 'String'
dictionary["results"]![0]!["address"]
我在「let address = ...」行有「找不到成員'下標'」。
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.chain!.getAddress(address!) { dictionary, error in
NSLog("%@", dictionary)
let address = dictionary["results"]![0]["address"]!
print("address: \(address)")
}
訪問容器內容不解析。 JSON是文本,將其轉換爲集合對象的過程就是解析。 – zaph
由於我的回答不回答我刪除它的問題。 – zaph
你是在唱Swift 1.2還是在用Swift 2開發?我問這是因爲它在Swift 2中使用'guard'語句解析它有點整潔 –