0
我試圖解析JSON文件。第一級工作正常,但是當我想深入一步時,它不再工作。解析NSArray的斯威夫特卡倫特3
if let json = try JSONSerialization.jsonObject(with: ReceivedData, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary {
DispatchQueue.main.async(execute: {
let tokensLeft = json["tokensLeft"]
print("Tokens Left")
print(tokensLeft)
let product = json["products"]
print(product)
for i in 0 ..< (product as AnyObject).count {
let asin = product[i]["asin"] as? [[String:AnyObject]]
}
})
}
當我嘗試這樣的,我在值分配給ASIN收到此錯誤:‘任何’ 「類型有沒有標會員」
打印(產品)的值如下:
我已經嘗試過在這裏提供了一些解決方案,但毫無效果。它可能是數組中的數據有問題嗎?
我會很樂意爲每一個想法,你可以提供幫助解決這一問題。
感謝, 亞歷山大。
投你的產品[字符串:AnyObject]然後遍歷它不投關鍵「ASIN」設置爲[[字符串:AnyObject] – Raymond
非常感謝 - 這工作了罰款! –
不客氣。儘量避免使用NSDictionary和NSArray以及快速標準的代碼。 :-) https://engineering.vokal.io/iOS/CodingStandards/Swift.md.html https://github.com/raywenderlich/swift-style-guide – Raymond