如何使用woocommerce api中的SwiftyJson解析Json?(how to parsing Json with SwiftyJson from woocommerce api?)

编程入门 行业动态 更新时间:2024-10-25 14:27:46
如何使用woocommerce api中的SwiftyJson解析Json?(how to parsing Json with SwiftyJson from woocommerce api?)

我想使用alamofire和swiftyjson解析JSON

我试着像这样得到JSON(值)

let headers: HTTPHeaders = [ "Authorization": "Basic Y2tfZTA1ZGNmMDkwNTNmODEyMGQwYTMyOGI4YzJkY2QzOTY5MmE5ZDAyNzpjc18zYzZiYWY2NTM0NDhkNDM4ZDM1ZDNmNDY5Nzg5ZGM2Y2VhZGRiZjNl", "Accept": "application/json" ] Alamofire.request("https://woo.demoapp.xyz/wp-json/wc/v2/products?category=15", headers: headers).responseJSON { response in debugPrint(response) if let json = response.result.value { print("JSON: \(json)") } }

这是来自woocommerce api的JSON数据

[ { "id": 29, "name": "Sunglasses", "permalink": "https://woo.demoapp.xyz/product/sunglasses/", "description": "<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>\n", "images": [ { "id": 17, "src": "https://woo.demoapp.xyz/wp-content/uploads/2017/10/sunglasses.jpg", "name": "Sunglasses", } ],

问题是我无法使用我认为正确的代码来填充数组,以使用swiftyJSON解析到tableview的JSON文件。

I want to parse JSON using alamofire and swiftyjson

I try get JSON(value) like this

let headers: HTTPHeaders = [ "Authorization": "Basic Y2tfZTA1ZGNmMDkwNTNmODEyMGQwYTMyOGI4YzJkY2QzOTY5MmE5ZDAyNzpjc18zYzZiYWY2NTM0NDhkNDM4ZDM1ZDNmNDY5Nzg5ZGM2Y2VhZGRiZjNl", "Accept": "application/json" ] Alamofire.request("https://woo.demoapp.xyz/wp-json/wc/v2/products?category=15", headers: headers).responseJSON { response in debugPrint(response) if let json = response.result.value { print("JSON: \(json)") } }

here is the JSON data from woocommerce api

[ { "id": 29, "name": "Sunglasses", "permalink": "https://woo.demoapp.xyz/product/sunglasses/", "description": "<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>\n", "images": [ { "id": 17, "src": "https://woo.demoapp.xyz/wp-content/uploads/2017/10/sunglasses.jpg", "name": "Sunglasses", } ],

The issue is i am not able to populate an array with what I thought would be correct code to parse a JSON file with swiftyJSON in to tableview.

最满意答案

请使用此类型

let headers: HTTPHeaders = [ "Authorization": "Basic Y2tfZTA1ZGNmMDkwNTNmODEyMGQwYTMyOGI4YzJkY2QzOTY5MmE5ZDAyNzpjc18zYzZiYWY2NTM0NDhkNDM4ZDM1ZDNmNDY5Nzg5ZGM2Y2VhZGRiZjNl", "Accept": "application/json" ] Alamofire.request("https://woo.demoapp.xyz/wp-json/wc/v2/products?category=15", headers: headers).responseJSON { response in debugPrint(response) if let json = response.result.value { print("JSON: \(json)") let swjson = JSON(response.result.value!) print(swjson) // callback(swjson,nil) var myMutableDictionary = [AnyHashable: Any]() myMutableDictionary["myArray"] = swjson let sss = JSON(myMutableDictionary as Any) let arrdata = sss["myArray"].arrayObject var productArray = NSArray() productArray = arrdata as! [[String:AnyObject]] as NSArray print(productArray.count) yourtableview.reload() } }

//表视图方法

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return productArray.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomeCell let dic = productArray[indexPath.row] as! NSDictionary let name = dic.object(forKey: "name") as! String return cell }

please use this type

let headers: HTTPHeaders = [ "Authorization": "Basic Y2tfZTA1ZGNmMDkwNTNmODEyMGQwYTMyOGI4YzJkY2QzOTY5MmE5ZDAyNzpjc18zYzZiYWY2NTM0NDhkNDM4ZDM1ZDNmNDY5Nzg5ZGM2Y2VhZGRiZjNl", "Accept": "application/json" ] Alamofire.request("https://woo.demoapp.xyz/wp-json/wc/v2/products?category=15", headers: headers).responseJSON { response in debugPrint(response) if let json = response.result.value { print("JSON: \(json)") let swjson = JSON(response.result.value!) print(swjson) // callback(swjson,nil) var myMutableDictionary = [AnyHashable: Any]() myMutableDictionary["myArray"] = swjson let sss = JSON(myMutableDictionary as Any) let arrdata = sss["myArray"].arrayObject var productArray = NSArray() productArray = arrdata as! [[String:AnyObject]] as NSArray print(productArray.count) yourtableview.reload() } }

//table view method

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return productArray.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomeCell let dic = productArray[indexPath.row] as! NSDictionary let name = dic.object(forKey: "name") as! String return cell }

更多推荐

本文发布于:2023-07-26 00:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1268314.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   woocommerce   api   parsing   Json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!