Swift 3中的JSONSerialization错误与unicode字符

时间:2016-09-09 10:23:02

标签: json swift swift3

我正在尝试解析类似于此的JSON对象:

{"topics":[{"tid":24909,"uid":400,"cid":"3","mainPid":790889,"title":"Retrasados just being retrasados una vez mOptional("\u{00E1}")s en Galicia","slug":"24909/retrasados-just-being-retrasados-una-vez-mOptional("\u{00E1}")s-en-galicia","timestamp":1473334344094,"lastposttime":1473414364688,"postcount":23,"viewcount":95,"locked":false,"deleted":false,"pinned":false,"teaserPid":"791474","titleRaw":"Retrasados just being retrasados una vez mOptional("\u{00E1}")s en Galicia"}]}

使用此Swift 3代码:

```

do{ 

       let json = try JSONSerialization.jsonObject(with: cleanData.data(using: String.Encoding.ascii)!, options: .allowFragments) as? Dictionary<String, AnyObject>

        nextStart = (json!["nextStart"] as? Int)!
        let topics = json!["topics"] as? [Dictionary<String, AnyObject>]

        for t in topics!{
            //print(t["tid"])
            let topic = Thread(threadDic: t)
            //print(topic.title)
            self.topics.append(topic)
        }

        // Main UI Thread
        DispatchQueue.main.async(execute: { () -> Void in
            self.tableView.reloadData()
            self.indicator.stopAnimating()
        })

    }catch{
        print("ERROR: \(error)")
    }

```

但是它给了我以下错误:Error Domain = NSCocoaErrorDomain Code = 3840“字符122周围形成的对象很糟糕。” UserInfo = {NSDebugDescription =字符122周围形成错误的对象。}

我在Swift 2.2中使用了这个代码,但现在它不起作用..我认为错误来自unicode特殊字符,如¿,ñ.. 如您所见,所有都是可选的(“\ u {...}”)。

任何人都知道如何解决它?我是斯威夫特的新人。

0 个答案:

没有答案