Swift3 如何使用来自woocommerce api的SwiftyJson解析Json?

Swift3 如何使用来自woocommerce api的SwiftyJson解析Json?,swift3,alamofire,swifty-json,Swift3,Alamofire,Swifty Json,我想使用alamofire和swiftyjson解析JSON 我尝试像这样获取JSON(值) let headers: HTTPHeaders = [ "Authorization": "Basic Y2tfZTA1ZGNmMDkwNTNmODEyMGQwYTMyOGI4YzJkY2QzOTY5MmE5ZDAyNzpjc18zYzZiYWY2NTM0NDhkNDM4ZDM1ZDNmNDY5Nzg5ZGM2Y2VhZGRiZjNl", "Accept": "appl

我想使用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)")

        }

    }
下面是来自WooCommerceAPI的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",

        }
    ],
[
{
“id”:29,
“名称”:“太阳镜”,
“永久链接”:https://woo.demoapp.xyz/product/sunglasses/",
“描述”:“佩伦特式居住者莫比·特里斯蒂克·塞内特斯和马莱苏达·埃吉斯塔斯·埃吉斯塔斯·埃吉斯塔斯·埃吉斯·埃吉斯·埃吉斯·埃吉斯·埃吉斯·埃吉斯·埃吉斯·埃吉斯塔斯·埃吉斯塔斯·埃吉斯塔斯·埃吉斯塔斯·埃吉斯塔斯·埃吉斯塔斯·埃吉斯塔斯·埃吉斯塔斯·埃斯特、埃吉斯·埃吉斯·埃吉斯·埃吉斯·埃吉斯·埃米特、临时埃米特·埃米特·埃米特、安特·埃斯·埃斯·,
“图像”:[
{
“id”:17,
“src”:https://woo.demoapp.xyz/wp-content/uploads/2017/10/sunglasses.jpg",
“名称”:“太阳镜”,
}
],
问题是我无法用我认为正确的代码填充数组,以便在tableview中使用swiftyJSON解析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)")
        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
}

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

    }


})

请搜索:并学习阅读JSON。这很简单:
{}
是字典,
[]
是数组。所有键都是字符串。双引号中的值是
String
,没有双引号的值是
Int
您想在表视图中填充吗?还是简单地说?我已经发布了答案,请使用此代码,我还测试了计数得到4。@BHAVIKPANCHAL我想在表视图中填充。如何实现?您能看到我的答案吗我发布的答案?如何解析图像中的“src”?上传图像有另一种方法和参数。