【发布时间】:2016-04-21 23:14:51
【问题描述】:
我在使用NSJSONSerialization 创建NSDictionary 的NSString 表示(JSON 字符串)时遇到了一些问题。我以前使用过 JSONKit,但由于它在 iOS9 中被弃用(并且崩溃),我切换到NSJSONSerialization。
这是我的代码:
// `userSettings` will be of type NSMutableDictionary*
NSData* data= [NSJSONSerialization dataWithJSONObject:userSettings options:0 error:&error];
NSString* settingsString= [NSString stringWithUTF8String:data.bytes];
currentUser.settings= settingsString; // NSString* property
现在,此代码有时会起作用,但有时settingsString 将是nil。当我在调试器中检查数据对象时,bytes 属性显示 JSON-String 后跟一些随机垃圾,如下所示:
1 = 0x00007ff1ba814000 "{\"residua
...
lculatePlanned\":\"0\",\"wizardUserId\":\"\"}UITextColor\x91UISystemColorName\x94UIBaselineAdjustment\x8cUISystemFont\x89NS.intval\x8eUIShadowOffset\x94UIAutoresizeSubviews\x8dUIContentMode\x85NSRGB\x8aUIFontName\x8bUITextLabel\x8eNSInlinedValue\x91UIDetailTextLabel\x99UIUserInteractionDisabled\x9dUITableCellBackgroundColorSet\x94UINibEncoderEmptyKey\x87NSWhite\x8cNSColorSpace\x8fUITextAlignment\xa3UINibAccessibilityConfigurationsKey\x92UIAutoresizingMask\x99UIProxiedObjectIdentifier\x87UIAlpha\x87UIWhite\x9aUIFontDescriptorAttributes\x8cUIFontTraits\x86NSSize\x95UIColorComponentCount\x91UIMinimumFontSize\x86UIText\x96UIMultipleTouchEnabled\x8dUIDestination\x94UIMi..."
^ start of garbage after end of dictionary
我做错了什么?
【问题讨论】:
标签: ios json nsdictionary ios9 nsjsonserialization