我遇到了麻烦更新我的应用程序作为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