如何用alamofire和swiftjson解析json?

时间:2019-01-05 03:17:06

标签: xcode

我正在使用swiftjson和alamofire解析json.pls,有助于修复我的代码。

here the json array

下面是我的代码

    RestClient.sharedClient.getUserWallet(uid: uid!, token: token!) { (json, error) in
        print(json as Any)

        self.tableRows = json!["data"].count

        //ALL BELOW IS DONE BY SWIFTYJSON
        let jsonArray = json?["data"].array

        if jsonArray != nil {
            for object in jsonArray! {
                let walletCells: UserWalletCells = UserWalletCells(json: object)
                self.userWalletCells.append(walletCells)
            }
        }

class PackageList {

var id: Int?
var user_id: String?
var amount: Int?
var currency_id: Int?
var release_rate: Double?
var release_per_day: Int?
var release_date: String?
var end_date: String?
var total_release: Int?
var tier_id: Int?
var is_active: Int?
var created: String?
var modified: String?
var remainingdays: Int?


init(json: JSON){


    self.id = json["id"].intValue
    self.user_id = json["user_id"].stringValue
    self.amount = json["amount"].intValue
    self.currency_id = json["currency_id"].intValue
    self.release_rate = json["release_rate"].doubleValue
    self.release_per_day = json["release_per_day"].intValue
    self.release_date = json["release_date"].stringValue
    self.end_date = json["end_date"].stringValue
    self.total_release = json["total_release"].intValue
    self.tier_id = json["tier_id"].intValue
    self.is_active = json["is_active"].intValue
    self.created = json["created"].stringValue
    self.modified = json["modified"].stringValue
    self.remainingdays = json["remainingdays"].intValue

}
}

我正在使用swiftjson和alamofire解析json.pls,有助于修复我的代码。

1 个答案:

答案 0 :(得分:0)

请尝试此操作对您有帮助

       let json = JSON(response)
       let dictResult = json["data"].dictionaryValue
       let arrayValue = dictResult["apckage"].arrayValue
        var arrList = [PackageList]()
        for i in 0..<arrayValue.count{
            let packageList                        = PackageList()
            let dictRespose                      = arrayValue[i]
            packageList. id = dictRespose["id"].stringValue
            packageList. user_id = dictRespose["user_id"].stringValue
            packageList. amount = dictRespose["amount"].stringValue      
            arrList.append(packageList)
        }
        return arrTaxLis

像这样,您将所有密钥都放入了。它可能会帮助您。谢谢。