首页 > 解决方案 > 从数组中获取数据以响应 Swift

问题描述

我正在访问一个 API,响应显示如下,

 "responseBody": {
    "data": [
        "{\"AED_USD\":0.272255,\"USD_AED\":3.67305}"
    ]
}

我很困惑如何从这个数组数据中取出 AED_USD 和 USD_AED 的值。我尝试将所有响应放入数组中,并尝试从索引库中获取值,但不起作用。我怎样才能获得价值?我的代码是这样的,

 let params = ["sourceCurrency":self.accountFromTxt.text!,
                  "targetCurrency":self.accountToTxt.text!] as [String : AnyObject]

    print(params)

    APIService.ExchangePrice(showActivityIndicator: true, parameters: params) { (responseObject) in
        if (responseObject?.status)!
        {
            self.print(responseObject?.data)

            self.exchangeRateArray.removeAll()

            if let usersDataArray = responseObject?.data as? [[String : Any]] {
                for userData in usersDataArray {
                    self.exchangeRateArray.append(ExchangeRateCurrency(JSON:userData)!)
                }

                if usersDataArray.count == 0
                {
                    //Empty Message
                    self.view.showEmptyScreenMessage(text: EmptyScreenMessages.transactionDetailMessage)
                }

                self.print(self.exchangeRateArray.count,self.exchangeRateArray[0])
            }
        }
        else
        {
            Utilities.showBar(text: responseObject?.errorObject?.message)
        }
    }

标签: jsonswift

解决方案


您的数据是字符串形式,将字符串更改为 JSON NSditonary。尝试像这样转换

let str = "{\"AED_USD\":0.272257,\"USD_AED\":3.673001}"
        if let data = str.data(using: String.Encoding.utf8) {
            do {
                let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String:Any]
                print(json)
            } catch {
                print("Something went wrong")
            }
        }

// 在你的代码中。

 let params = ["sourceCurrency":self.accountFromTxt.text!,
                      "targetCurrency":self.accountToTxt.text!] as [String : AnyObject]

        print(params)

        APIService.ExchangePrice(showActivityIndicator: true, parameters: params) { (responseObject) in
            if (responseObject?.status)!
            {
                self.print(responseObject?.data)

                self.exchangeRateArray.removeAll()

                if let usersDataArray = responseObject?.data as? [[String : Any]] {
                    for userData in usersDataArray {
                     print(userData)
 // if you get string from userData
            if let data = userData.data(using: String.Encoding.utf8) {
                do {
                    let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String:Any]
                    print(json)
                } catch {
                    print("Something went wrong")
                }
            }
                    }

                    if usersDataArray.count == 0
                    {
                        //Empty Message
                        self.view.showEmptyScreenMessage(text: EmptyScreenMessages.transactionDetailMessage)
                    }

                    //self.print(self.exchangeRateArray.count,self.exchangeRateArray[0])
                }
            }
            else
            {
                Utilities.showBar(text: responseObject?.errorObject?.message)
            }
        }

推荐阅读