繁体   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