gpt4 book ai didi

json - 如何使用 SwiftJSON 将 JSON 转换为另一种类型

转载 作者:行者123 更新时间:2023-11-30 14:04:46 25 4
gpt4 key购买 nike

我尝试使用 Alamofire + SwiftJSON 从 Web 服务获取 json 数据,但无法将 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 数据输入到 Product:[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
}
}
}

按照上面的代码获取您的解决方案

关于json - 如何使用 SwiftJSON 将 JSON 转换为另一种类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32557472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com