我想从响应中解析JSON。但我得到这个错误
我的代码是。无法将'__NSCFString'(0x104a67320)类型的值转换为'NSDictionary'(0x104a68108)
NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data, response, error) in
if error != nil {
print("error = \(error)")
return
}
do {
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("responseString = \(responseString)")
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments) as! NSDictionary
let str = json["ResultMsg"] as! NSString
print ("ResultMsg = " + (str as String))
}catch let error as NSError {
print("Error : " + error.localizedDescription)
}
}).resume()
和控制台日志是:
responseString =可选( “{\” ResuleCode \ “:\” 1 \ “\ ”ResultMsg \“:\ ”SUCCESS \“,\”结果\ “:[{\” UserIdx \ “:\” 4138 \ “\ ”电子邮件\“:\ ”[email protected] \“,\ ”用户名\“:\ ”测试1 \“,\” PWD \ “:\” v + Mb90ZS + Y5Qt9DfuBcJtQ == \ “\ ”电话\“:\ ”\“,\ ”的AppKey \“:\ ”\“,\ ”LoginDt \“:\” 9/23/2016 1:12:18 PM \“,\”LogoutDt \“:\”2016/9/1 10:36:02 \“,\”RegDt \“:\”2016/8/31 9:26: “\”,\“KeywordTime \”:\“60 \”,\“KeywordEnabled \”:\“1 \”,\“FastEnabled \”:\“1 \ ”,\ “KeywordStartTime \”:\ “08:30:00 \”,\ “KeywordEndTime \”:\ “15:30:00 \”,\ “FastStartTime \”:\ “08:00:00 \”, \ “FastEndTime \”:\ “15:30:00 \”,\ “IsFirst \”:\“NOT_FIR ST \”,\ “IsFastNews \”:\ “1 \” \ “IsKeywordNews \”:\ “1 \” \ “KeywordStartDt \”:\ “\” \ “KeywordEndDt \”:\ “\”} ]}“)
无法将'__NSCFString'(0x10abdf320)类型的值转换为'NSDictionary'(0x10abe0108)。
任何帮助将不胜感激!
放断点,并检查其行抛出这个错误? –
让json =试试NSJSONSerialization.JSONObjectWithData(data !, options:.AllowFragments)as! NSDictionary - >这一行是问题..信号SOGABRT –
@MoonJikLee你可以显示你的实际JSON响应,而不是显示我们的Xcode响应。 –