how to parsing Json with SwiftyJson from woocommerce api?

谁说我不能喝 提交于 2019-12-02 08:32:45

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
}

Upload Image With Parameter Using Alamofire

let parameters = ["category":"15" as AnyObject]

let headers: HTTPHeaders = [
    "Authorization": "Basic Y2tfZTA1ZGNmMDkwNTNmODEyMGQwYTMyOGI4YzJkY2QzOTY5MmE5ZDAyNzpjc18zYzZiYWY2NTM0NDhkNDM4ZDM1ZDNmNDY5Nzg5ZGM2Y2VhZGRiZjNl",
    "Accept": "application/json"
]

Alamofire.upload(multipartFormData: { multipartFormData in

    multipartFormData.append(fileUrl , withName: "image" , fileName: yourfilename + ".yourfiletype", mimeType: "yourfiletype")

    for (key, value) in parameters {
        multipartFormData.append(value.data(using: String.Encoding.utf8.rawValue)!, withName: key)
    }

},to: "https://woo.demoapp.xyz/wp-json/wc/v2/products", method: .post, headers: headers ,
  encodingCompletion: { encodingResult in
    switch encodingResult {
    case .success(let upload, _, _):
        upload.response { [weak self] response in
            guard self != nil else {
                return
            }

        }

        upload.responseJSON {  response in

            let responseJSON = response.result.value as! NSDictionary


        }

    case .failure(let encodingError):
        print("error:\(encodingError)")

    }


})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!