簡體   English   中英

從json響應swift中刪除數組中的雙引號

[英]remove double quotes in array from json response swift

我正在使用 Alamofire 進行 url 調用以獲取如下數據。

在第一步中,我將 JSON 數組轉換為這種格式

[“a”、“b”、“b”、“c”]

並且它的工作正常。 _price變量的問題是這樣的

[“1233”、“1333”、“3422”、“2422”]

但我需要從_price數組中刪除雙引號,最終_price像這樣顯示

[1233、1333、3422、2422]

class ChartVC: UIViewController {

    var _year : [String] = []
    var _month : [String] = []
    var _price : [String] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        getData()

    }

    func getData() {
        AF.request(DOLLAR_CHART).response { (response) in
            guard let data = response.data else { return }
            let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
            if let items = responseJSON as? [[String: Any]] {
                var years: [String] = []
                var months: [String] = []
                var prices: [String] = []
                for item in items {
                    if let year =  item["year"] as? String  {
                        years.append(year)                            
                    }
                    if let month = item["month"] as? String {
                        months.append(month)
                    }
                    if let price = item["price"] as? String {
                        prices.append(price)
                    }

                }

                self._year = years
                self._month = months
                self._price = prices
                print(self._price)

                // show like this when print that["1233", "1333","3422","2422"]
                // how to show like this [1233, 1333, 3422, 2422]
                }
            } else {
                print("json is not array dictionary")
            }
        }
    }

如果您想從價格中獲得整數值。

if let price = Int(item["price"] as? String ?? ""){
   prices.append(price)
 }

並且您應該將 _price 和價格數組定義為 Int 數組。

var _price : [Int] = []
var prices: [Int] = []

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM