2016-09-26 67 views
9

我在Alamofire 4中遇到了特殊字符的問題。
JSON包含æ,ø和å,瀏覽器顯示它們正常,我以前的解決方案也使用SwiftyJSON。

Alamofire 4顯示了這樣的事情,而不是:Alamofire 4和JSON中的特殊字符

U00e6 

使用此電話:

Alamofire.request(specificURL, method: .get, parameters: param, encoding: URLEncoding.default, headers: nil).responseJSON { (response: DataResponse<Any>) in 
    print(response) 
} 

怎樣做才能解決這個問題?

+2

你確定你的JSON是正確編碼爲utf8? –

+0

是的,因爲瀏覽器和我以前的解決方案與SwiftyJSON顯示它沒問題。 – Recusiwe

+1

@Recusiwe檢查我的答案。 –

回答

6

編輯:

Alamofire.request(url, method: .get, parameters: param, encoding: JSONEncoding.default) 
     .responseJSON { response in 

      switch response.result { 
      case .success(let value) : 

       print(response.request) // original URL request 
       print(response.response) // HTTP URL response 
       print(response.data)  // server data 
       print(response.result) // result of response serialization 

       if let JSON = response.result.value as! [String:AnyObject]!{ 
        print("JSON: ",JSON) 
        self.arrUser = Mapper<Users>().mapArray(JSONArray:JSON["user"] as! [[String : Any]])! 
        self.tableView.reloadData() 
       } 
      case .failure(let encodingError): 
       //Print error 
      } 
    } 

我得到了我在JSON響應加入AE和嘗試打印問題。

輸出:

JSON: Optional(<__NSArrayI 0x600000050320>(
{ 
    "email_address" = "[email protected]"; 
    username = "testwts06 \U00e6"; 
}, 
{ 
    "email_address" = "[email protected]"; 
    username = "testwts01 \U00eb"; 
}, 
{ 
    "email_address" = "[email protected]"; 
    username = testwts100; 
}) 

雖然顯示它在正確的格式顯示。

image

+0

我似乎無法找到上述成員?我的具體URL只是一個字符串。 – Recusiwe

+0

@Recusiwe我已經更新了我的答案。請檢查 –

+0

當我將編碼設置爲JSONEncoding時,它在此Alamofire.request(specificURL,方法:.get,參數:param,編碼:JSONEncoding.default)中不打印任何內容。print( response.result) } } '我在瀏覽器中檢查了網址,它都很好。 – Recusiwe

1

爲的Ekta的回答斯威夫特3更新:

let encodedURL = specificURL.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed) 
1

似乎是典型的序列化錯誤由於錯誤的JSON編碼,可能是你的響應狀態代碼是3840

JSON文本應以UTF-8,UTF-16或UTF-32編碼。

你可以嘗試將響應數據轉換爲正確的UTF8編碼:

let datastring = NSString(data: response.data!, encoding: String.Encoding.isoLatin1.rawValue) 
let data = datastring!.data(using: String.Encoding.utf8.rawValue) 
do { 
    let object = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) 
    let jsonDic = object as! NSDictionary 
    print(" ok, successfully converted..\(jsonDic)") 
} catch let aError as Error { 
    // print and handle aError 
} 

希望它可以幫助你。

-1

下面是一個簡單String擴展,解決了這個問題:

extension String { 
    func fixUnicode() -> String { 
     var copy = self as NSString 
     let regex = try! NSRegularExpression(pattern: "\\\\U([A-Z0-9]{4})", options: .caseInsensitive) 
     let matches = regex.matches(in: self, options: [], range: NSMakeRange(0, characters.count)).reversed() 
     matches.forEach { 
      let char = copy.substring(with: $0.rangeAt(1)) 
      copy = copy.replacingCharacters(in: $0.range, with: String(UnicodeScalar(Int(char, radix: 16)!)!)) as NSString 
     } 
     return copy as String 
    } 
}