我有一个带有 Json 的 tableView 并且工作完美,但是今天我们更新了 Json,现在无法工作。我遇到这个问题:
Error with Json: Error Domain=NSCocoaErrorDomain Code=3840 "Unable to convert data to string around character 5060." UserInfo={NSDebugDescription=Unable to convert data to string around character 5060.}
我找到了这篇文章,我认为可行,但是 Obj-c NSJSONSerialization - Unable to convert data to string
let requestURL: NSURL = NSURL(string: rutaJSon)!
let urlRequest: NSMutableURLRequest = NSMutableURLRequest(URL: requestURL)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(urlRequest) {
(data, response, error) -> Void in
let httpResponse = response as! NSHTTPURLResponse
let statusCode = httpResponse.statusCode
if (statusCode == 200) {
self.arrayNombre.removeAll()
self.arrayTel.removeAll()
self.arrayMov.removeAll()
do{
let json = try NSJSONSerialization.JSONObjectWithData(data!, options:.AllowFragments)
//print(json)
有人可以帮助我吗?谢谢
最佳答案
您的服务器似乎没有使用正确的 JSON 编码类型之一。来自文档:
The data must be in one of the 5 supported encodings listed in the JSON specification: UTF-8, UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE. The data may or may not have a BOM. The most efficient encoding to use for parsing is UTF-8, so if you have a choice in encoding the data passed to this method, use UTF-8.
您的服务器需要对数据进行正确编码。
关于json - NSJSONSerialization - 无法在 Swift 上将数据转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37189440/