无法将数据转换为字符周围的字符串 [英] Unable to convert data to string around character
问题描述
我正在尝试发出HTTP-Get请求以使用户进入我的数据库.从请求返回数据后,我使用 SwiftyJson库来获取用户.
i'm trying to do an HTTP-Get request to get users on my database. After the data return from the request i use SwiftyJson library to get the users.
但是,以他的名字命名的用户之一包含字符ò",并且我收到了以下错误消息:
Optional(Error Domain=NSCocoaErrorDomain Code=3840 "Unable to convert data to string around character 208."
UserInfo={NSDebugDescription=Unable to convert data to string around character 208.})
But, one of the users in his name contains the character "ò" and i received this error:
Optional(Error Domain=NSCocoaErrorDomain Code=3840 "Unable to convert data to string around character 208."
UserInfo={NSDebugDescription=Unable to convert data to string around character 208.})
这是我的代码:
func makeHTTPGetRequest(path: String, onCompletion: ServiceResponse) {
let request = NSMutableURLRequest(URL: NSURL(string: path)!)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
if let jsonData = data {
var parseError: NSError?
let json:JSON = JSON(data: jsonData, error: &parseError)
onCompletion(json, parseError)
} else {
onCompletion(nil, error)
}
})
task.resume()
}
推荐答案
如果我理解此问题,我认为您必须将servlet的输出设置为UTF-8. 例如,如果您使用Java Servlet,则可以使用
If i understood the problem, i think that you have to set the output of your servlet to UTF-8. An example, if you use a Java Servlet you can use
response.setCharacterEncoding("UTF-8");
这篇关于无法将数据转换为字符周围的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!