Objective c 从NSDictionary生成JSON字符串会引发异常
我试图用以下方式生成json字符串:Objective c 从NSDictionary生成JSON字符串会引发异常,objective-c,json,macos,Objective C,Json,Macos,我试图用以下方式生成json字符串: NSData* jsonData = [NSJSONSerialization dataWithJSONObject:JSONDictionary options:NSJSONWritingPrettyPrinted error:&error]; NSString* json = nil; if (! jsonData) { NSLog(@"Got an error: %@", error); } else { json = [[NS
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:JSONDictionary options:NSJSONWritingPrettyPrinted error:&error];
NSString* json = nil;
if (! jsonData) {
NSLog(@"Got an error: %@", error);
} else {
json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
其中JSONDictionary初始化为:
NSDictionary *JSONDictionary = [NSDictionary dictionaryWithObjectsAndKeys: dateString, @"start", [self.currentlyConnectedPeripheral UUID], @"device_id", [self.currentlyConnectedPeripheral name], @"device_name", rrs, @"rates", @"123", @"id", nil];
但我的应用程序引发了一个例外:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (__NSCFType)' ***
我的问题是什么?如何使其工作?好的,
[self.currentlyConnectedPeripheral UUID]
返回什么类型?什么是dateString
,[self.currentlyConnectedPeripheral UUID]
,[self.currentlyConnectedPeripheral name]
和rrs
。其中一个不是可以JSON序列化的类型(其中一个将是\uu NSCFType
!)[self.currentlyConnectedPeripheral UUID]返回CBUUIDRef对象,可能这是我的代码的真正问题,感谢您指出这一点。它清楚地告诉您字典中有一个无效的(用于JSON)对象。对象只能是NSString、NSNumber、NSNull、NSDictionary或NSArray。把你的字典记录下来,看看里面有什么。