本文介紹了如何將 JSON 字符串反序列化為 NSDictionary?(適用于 iOS 5+)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
在我的 iOS 5 應用程序中,我有一個包含 JSON 字符串的 NSString
.我想將該 JSON 字符串表示反序列化為本機 NSDictionary
對象.
In my iOS 5 app, I have an NSString
that contains a JSON string. I would like to deserialize that JSON string representation into a native NSDictionary
object.
"{"password" : "1234", "user" : "andreas"}"
我嘗試了以下方法:
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:@"{"2":"3"}"
options:NSJSONReadingMutableContainers
error:&e];
但它會引發運行時錯誤.我做錯了什么?
-[__NSCFConstantString bytes]: unrecognized selector sent to instance 0x1372c
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[__NSCFConstantString bytes]: unrecognized selector sent to instance 0x1372c'
推薦答案
看起來你正在傳遞一個 NSString
參數,而你應該傳遞一個 NSData
參數:
It looks like you are passing an NSString
parameter where you should be passing an NSData
parameter:
NSError *jsonError;
NSData *objectData = [@"{"2":"3"}" dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
options:NSJSONReadingMutableContainers
error:&jsonError];
這篇關于如何將 JSON 字符串反序列化為 NSDictionary?(適用于 iOS 5+)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!