我尝试使用Alamofire + SwiftJSON获取json数据形式的Web服务,但我无法将类型JSON转换为其他类型
我的代码
var product:JSON = []
override func viewDidLoad()
{ Alamofire.request(.GET, URL).responseJSON { (request, response, json, error) in
if json != nil {
self.product = JSON(json!)
let data = self.product["product"]
self.product = JSON(data!)
我想在产品中使用json数据:[NSDictionary],用于在我的collectionViewCell中显示标题名称或id。请一些建议。 -
Json数据响应
{
"product":{
"title":"Night Cream",
"id":4573,
"created_at":"2015-08-21T07:54:09Z",
"updated_at":"2015-08-27T01:37:06Z",
}
}
如何将数据(类型JSON)转换为字典以便申请将来使用?
var product:[String:JSON] = [String:JSON]()
override func viewDidLoad()
{
Alamofire.request(YOUR_URL, method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in
switch(response.result)
{
case .success(_):
let dict = JSON(response.result.value ?? "").dictionaryValue
self.product=dict["product"].dictionaryValue
break
case .failure(_):
// The network connection was lost please try again
break
}
}
}
按照上面的代码来获得您的解决方案