2016-09-26 66 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 
    } 
}