我想從響應中解析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響應。 –