首页 文章

无法将'__NSCFString'(0x104a67320)类型的值转换为'NSDictionary'(0x104a68108)

提问于
浏览
-2

我试图从响应中解析JSON . 但我得到了这个错误
我的代码是 .

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 \“ ,“电子邮件”:\“testapi@test.co.kr \”,\“UserName \”:\“test1 \”,\“Pwd \”:\“v Mb90ZS Y5Qt9DfuBcJtQ == \”,\“电话\“:\”012345678913 \“,\”AppKey \“:\”\“,\”LoginDt \“:\”9/23/2016 1:12:18 PM \“,\”LogoutDt \“:\” 9/1/2016 10:36:02 AM \“,\”RegDt \“:\”8/31/2016 9:26:04 AM \“,\”DelDt \“:\”\“,\”KeywordTime \ “:\” 60 \”,\ “KeywordEnabled \”:\ “1 \” \ “FastEnabled \”:\ “1 \” \ “KeywordStartTime \”:\ “8点30分00秒\”,\ “KeywordEndTime \”:\ “15:30:00 \”,\ “FastStartTime \”:\ “08:00:00 \”,\ “FastEndTime \”:\ “15:30:00 \”,\“IsFirst \ “:\” NOT_FIRST \”,\ “IsFastNews \”:\ “1 \” \ “IsKeywordNews \”:\ “1 \” \ “KeywordStartDt \”:\ “\” \ “KeywordEndDt \”: \“\”}]}“)无法将类型'__NSCFString'(0x10abdf320)的值转换为'NSDictionary'(0x10abe0108) .

任何帮助将不胜感激!

3 回答

  • 0

    我发现这段代码有效 .

    if let responseData = responseString!.dataUsingEncoding(NSUTF8StringEncoding)
    {
      let json = try NSJSONSerialization.JSONObjectWithData(responseData, options: .AllowFragments)                
      print(json)
    }
    

    但我完全不知道原因..无论如何,谢谢大家

  • -1

    试试此代码:

    let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSMutableDictionary
    
  • 0

    试试这个:

    do{
        if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments) as! NSDictionary
    
        print("parse success")
    }catch{
        print("parse error")
    }
    

相关问题