如何从woocommerce api使用SwiftyJson解析Json? [英] how to parsing Json with SwiftyJson from woocommerce api?

查看:124
本文介绍了如何从woocommerce api使用SwiftyJson解析Json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用alamofire和swiftyjson解析JSON

I want to parse JSON using alamofire and swiftyjson

我尝试获取这样的JSON(value)

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)")

        }

    }

这是woocommerce api的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
}

这篇关于如何从woocommerce api使用SwiftyJson解析Json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆