在Swift 4中通过JSON URL加载阿拉伯语数据

时间:2018-07-19 10:04:04

标签: ios json swift alamofire

`

    var Cities:[String] = [String]()
    let requestURL =  "https://----".addingPercentEncoding(withAllowedCharacters:CharacterSet.urlQueryAllowed)!

    //Sending http post request
    Alamofire.request(requestURL).responseJSON { (responseData) -> Void in
        if((responseData.result.value) != nil) {
            let swiftyJsonVar1 = JSON(responseData.result.value!)
            let strjson = swiftyJsonVar1.rawString()!.data(using: String.Encoding.utf8)!
                let swiftyJsonVar = JSON(strjson)
                if let resData = swiftyJsonVar["Cities"].arrayObject {
                    print(swiftyJsonVar["Cities"].arrayObject! )
                    for a in resData {
                        let v = a as! [String:Any]
                        let City = v["Name"]!
                        Cities.append(City as! String)
                    }
                    self.TFrom.filterStrings(Cities) // put data from array to Component AutoComplete
                    self.TFrom.startVisible = true
                    self.TFrom.maxNumberOfResults = 5
                    self.TTo.filterStrings(Cities) // put data from array to Component AutoComplete
                    self.TTo.startVisible = true
                    self.TTo.maxNumberOfResults = 5

                }
            }
    }

` 当使用JSON时,URL具有英语数据,这很好,但是用阿拉伯语返回空,有什么帮助吗? swift code to show data in url

1 个答案:

答案 0 :(得分:0)

您需要设置Content-Type标头以请求:

var requestURL = URLRequest(url: URL(string: "https//:wwww.mywebsite.com")!)
requestURL.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")