In Swift 3, the return type of JSONSerialization.jsonObject(with:options:)
has become Any
.
(You can check it in the Quick Help pane of your Xcode, with pointing on jsonResult
.)
And you cannot call any methods or subscripts for the variable typed as Any
. You need explicit type conversion to work with Any
.
if let jsonResult = jsonResult as? [String: Any] {
print(jsonResult["team1"])
}
And the default Element type of NSArray
, the default Value type of NSDictionary
have also become Any
. (All these things are simply called as "id-as-Any", SE-0116.)
So, if you want go deeper into you JSON structure, you may need some other explicit type conversion.
if let team1 = jsonResult["team1"] as? [String: Any] {
print(team1["a"])
print(team1["b"])
print(team1["c"])
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…