无法转换类型' __ NSCFString' (0x104a67320)到' NSDictionary' (0x104a68108)

时间:2016-09-23 04:30:34

标签: ios swift

我正在尝试从响应中解析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 = Optional(" {\" ResuleCode \":\" 1 \",\" ResultMsg \":\& #34; SUCCESS \" \"结果\":[{\" UserIdx \":\" 4138 \" \& #34;电子邮件\":\" testapi@test.co.kr \" \"用户名\":\" TEST1 \&#34 ; \" PWD \":\" v + Mb90ZS + Y5Qt9DfuBcJtQ == \" \"电话\":\" 012345678913 \" \"的AppKey \":\" \" \" LoginDt \":\&#34 9/23 / 2016 1:12:18 PM \",\" LogoutDt \":\" 9/1/2016 10:36:02 AM \",\&# 34; RegDt \":\" 8/31/2016 9:26:04 AM \",\" DelDt \":\" \& #34; \" KeywordTime \":\" 60 \" \" KeywordEnabled \":\" 1 \&#34 ; \" FastEnabled \":\" 1 \" \" KeywordStartTime \":\" 08:30:00 \& #34; \" KeywordEndTime \":\" 15:30:00 \" \" FastStartTime \":\" 08 :00:00 \" \" FastEndTime \":\" 15:30:00 \" \" IsFirst \&#34 ;: \" NOT_FIRST \" \" IsFastNews \":\" 1 \" \" IsKeywordNews \":\& #34; 1 \&# 34; \" KeywordStartDt \":\" \" \" KeywordEndDt \":\" \"}] }&#34)
  无法转换类型' __ NSCFString'的值(0x10abdf320)到' NSDictionary' (0x10abe0108)。

非常感谢任何帮助!

3 个答案:

答案 0 :(得分:0)

试试这个:

do{
    if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments) as! NSDictionary

    print("parse success")
}catch{
    print("parse error")
}

答案 1 :(得分:0)

试用此代码:

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

答案 2 :(得分:-1)

我发现此代码有效。

String[] getActiveProfiles()
String[] getDefaultProfiles() //and 
boolean acceptsProfiles(String... profiles)

但是我不确切地知道原因..无论如何都要感谢