我遇到了麻煩更新我的應用程序作爲Alamofire和SwiftyJSON尚未支持斯威夫特3.我有一個網址,將返回我JSON如下:斯威夫特3 JSON解析
{
"products": [
{
"body_html":"",
"created_at":"2016-03-02T13:56:18+03:00",
"id":489759251,
"handle":"product",
"options":[
{
"id":627488838,
"product_id":489759251,
"name":"Title",
"position":1,
"values":[
"Default Title"
]
}
],
},
{
"body_html":"",
"created_at":"2016-03-08T05:17:55+03:00",
"id":530420915,
"handle":"product-2",
"options":[
{
"id":6319359750,
"product_id":530420915,
"name":"Title",
"position":1,
"values":[
"Default Title"
]
}
],
},
]
}
我需要能夠解析該json並列出所有返回的產品,並能夠讀取每個特定的屬性和子選項。
我在這裏查了一些其他的問題,發現了幾個解決方案,並能夠得到json數據並按照上面的方式打印出來。但是,我無法解析它。
let shopUrl = "https://\(apiKey):\(password)@\(hostname)" + "/admin/products.json"
let url = URL(string: shopUrl)
URLSession.shared.dataTask(with:url!, completionHandler: {(data, response, error) in
if error != nil {
print(error)
} else {
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String:Any]
print(json)
} catch let error as NSError {
print(error)
}
}
}).resume()
任何幫助?
這不是JSON,這是表示反序列化結果的「print」行的輸出。 – vadian
是的,沒錯。我認爲提供打印輸出會讓我更容易提供幫助 – Hamid
我知道它並不能幫助回答你的問題,但Alamofire4.0確實有Swift 3的支持。 SwiftyJSON沒有任何Swift 3的支持,除了一個早期beta版本的分支之外。 – donnywals