在我的iOS 5应用中,我有一个NSString包含JSON字符串的。我想将该JSON字符串表示形式反序列化为本地NSDictionary对象。
NSString
NSDictionary
"{\"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参数:
NSData
NSError *jsonError; NSData *objectData = [@"{\"2\":\"3\"}" dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData options:NSJSONReadingMutableContainers error:&jsonError];