json - NSJSONSerialization - 无法在 Swift 上将数据转换为字符串

标签 json swift

我有一个带有 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/

相关文章:

json - 如何从json数组中获取字符串(处理)

javascript - 如何使用jQuery Image Gallery插件加载加载本 map 片

javascript - d3.js 超出最大调用堆栈大小错误

ios - 左对齐图像并将文本放置在 UIButton 的中心

swift - CNContactVCardSerialization.data(带有 :) always returns nil result

swift - Firebase 存储与游戏数据数据库

php - JSON 的正确用法?

json - RESTful 集合资源 - 惯用的 JSON 表示和往返

swift - 在 Swift 中跳出内部 For 循环的方法

ios - swift 函数返回值未正确返回