2016-09-23 5 views
-2

応答から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() 

とコンソールログがある: ":\ "SUCCESS \"、\ "ResultMsg \" \" 1 \ "\"( "{\" ResuleCode \オプション

responseString = \ "ユーザーID \:\" 4138 \ "、\"電子メール\ ":\" [email protected] \ "、\"ユーザー名\:\ "テスト1 \"、\ " \ "Phone \":\ "\"、\ "AppKey \":\ "\"、\ "LoginDt \":\ "9/23/2016 1:12:18 PM \ "、\" LogoutDt \ ":\" 9/1/2016 10:36:02 AM \ "、\" RegDt \ ":\" 8/31/2016 9:26: \ "KeyEnabled \"、\ "KeyEnabled \":\ "KeyEnabled \":\ "1 \"、\ " \ "KeywordStartTime \":\ "08:30:00 \"、\ "KeywordEndTime \":\ "15:30:00 \"、\ "FastStartTime \":\ "08:00:00 \"、 \ "FastEndTime \":\ "15:30:00 \"、\ "IsFirst \":\ "NOT_FIR \ "IswordNews \"、\ "IsKeywordNews \"、\ "1 \"、\ "KeywordStartDt \":\ "\"、KeywordEndDt \ ":\" \ "} ]} ")
タイプ '__NSCFString'(0x10abdf320)の値を 'NSDictionary'(0x10abe0108)にキャストできませんでした。

ご協力いただければ幸いです。

+0

ブレークポイントを入れて、このエラーを投げているラインチェックを正確に理由を知らないのですか? –

+0

json = try NSJSONSerialization.JSONObjectWithData(data !, options:.AllowFragments)を! NSDictionary - >この行は問題です.. signal SOGABRT –

+0

@MoonJikLee xcodeレスポンスを表示する代わりに実際のjsonレスポンスを表示できますか? –

答えて

0

はこれを試してみてください:

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

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

解析エラーが発生したかどうかを確認してください。 –

+0

"if let son = xxxxx"行にエラーがあります。 "条件バインディングの初期化子には 'NSDoctionary'以外のオプションの型が必要です。 –

+0

as NSDictionaryまたはBetter as As String [AnyObject] –

0

は、このコードを試してみてください。

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

同じエラーが発生しました –

-1

私はこのコードが動作ました。

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

しかし、私は...おかげで、すべてとにかく

+0

これは私の回答に対処する正しい方法ではありません。 –

+0

最初にあなたは答えを求めています。それからあなたは私の答えとポスティングを対処しています。あなたの答えと私の違いは何ですか? –

関連する問題