本文介紹了如何將 JSON 字符串反序列化為 NSDictionary?(適用于 iOS 5+)的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
限時(shí)送ChatGPT賬號(hào)..
在我的 iOS 5 應(yīng)用程序中,我有一個(gè)包含 JSON 字符串的 NSString
.我想將該 JSON 字符串表示反序列化為本機(jī) NSDictionary
對(duì)象.
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];
但它會(huì)引發(fā)運(yùn)行時(shí)錯(cuò)誤.我做錯(cuò)了什么?
-[__NSCFConstantString bytes]: unrecognized selector sent to instance 0x1372c
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[__NSCFConstantString bytes]: unrecognized selector sent to instance 0x1372c'
推薦答案
看起來(lái)你正在傳遞一個(gè) NSString
參數(shù),而你應(yīng)該傳遞一個(gè) NSData
參數(shù):
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];
這篇關(guān)于如何將 JSON 字符串反序列化為 NSDictionary?(適用于 iOS 5+)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!