我想将 C 字符串解析为 NSDictionary。以下代码在 OS X 下编译时效果很好,但在带有 GNUStep 7.7 的 Ubuntu 14.04 LTS 下使用时会导致错误“JSON Parse error”。该错误仅发生在 bool 值(true、false)上,但适用于数字和字符串。
您知道问题可能是什么或者我如何解决它吗?
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//const char* str = "{\"result\":\"foo\"}"; //works
const char* str = "{\"result\":true}"; //doesn't work
NSData* data = [NSData dataWithBytes:str length:strlen(str)];
NSError *err = nil;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:&err];
if(err != nil)
//Output: "Err: JSON Parse error"
NSLog(@"Err: %@",err);
else
NSLog(@"Dic: %@",dic);
[pool drain];
return 0;
}
最佳答案
我找到了解决方案:这是我使用的 GNUstep 版本中的一个错误,当使用 latest source 时NSJSONSerialization
,一切正常。
关于objective-c - 使用 GNUStep 将 JSON 解析为 NSDictionary 对于 bool 值不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27197229/