迅速:更改设备语言后JSON无法正常工作

时间:2019-03-07 23:45:21

标签: json swift youtube

我发出JSON请求以获取goole YouTube搜索建议,如果我通过阿拉伯字母来作为搜索关键字,那么如果设备语言为英语,则效果很好,但是如果将其更改为阿拉伯语,则会收到以下消息:

  

JSON由于错误而无法序列化:   数据格式错误,无法读取。

代码:

        //Prepare Keyword string
    searchKeyword = (searchKeyword as NSString).replacingOccurrences(of: " ", with: "+")

    //Prepare url string
    var str: String = "http://suggestqueries.google.com/complete/search?ds=yt&client=firefox&hjson=t&q='\(searchKeyword)'"
    str = str.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!

    //Getting the data
    Alamofire.request(str).responseJSON { (response) in
        //If error
        if let error = response.error {
            print(error.localizedDescription)
        }
        //If successed
        if let jsonArray = response.result.value as? NSArray {
            //Use the data
        }
    }

备注: 当设备处于英文界面时,我在JSON响应说明中得到Content-Type = charset = ISO-8859-1 但是当设备处于阿拉伯语界面时Content-Type = charset = windows-1256

1 个答案:

答案 0 :(得分:0)

我添加了这一行,问题就解决了!

UserDefaults.standard.set(["ar", "en"], forKey: "AppleLanguages")