I'm using NSJSONSerialization dataWithJSONObject to serialize my classes to JSON. When it serializes a BOOL, it gives it the value 1 or 0 in the JSON string. I need this to be true or false instead. Is this possible to do generically?
我使用NSJSONSerialization dataWithJSONObject将类序列化为JSON。当它序列化BOOL时,它会在JSON字符串中给它值1或0。我需要这个为真或为假。这可能是一般的吗?
6 个解决方案
#1
3
No, the Foundation Object for a Bool is NSNumber
numberWithBool
, which becomes either 0 or 1. We have no Bool
Object. Same go for reading JSON
. True/false will become a NSNumber
again.
不,Bool的基对象是NSNumber withbool,它要么是0,要么是1。我们没有Bool对象。读取JSON也是一样。True/false将再次成为NSNumber。
You could create an Bool
Class and build your own parser. Arrays are Arrays and JSON
Objects are NSDictionary
. You can query the keys, test what Class lies behind and build the JSON
String from this.
您可以创建一个Bool类并构建自己的解析器。数组是数组,JSON对象是NSDictionary。您可以查询键,测试后面的类,并从中构建JSON字符串。
#2
22
When I create [NSNumber numberWithBool:NO]
, the NSJSONSerialization returns the word "false" in the JSON string.
当我创建[NSNumber withbool:NO]时,NSJSONSerialization返回JSON字符串中的单词“false”。
EDIT With the new shortcuts you can also use these handy guys:
使用新的快捷方式进行编辑,你也可以使用这些方便的工具:
@(YES) / @(NO)
@(1) / @(0)
@YES / @NO
@1 / @0
This way you can avoid something like looping through your values. I want the exact opposite behavior but have NSNumber
objects. So I have to loop...
通过这种方式,您可以避免像循环遍历您的值这样的事情。我想要完全相反的行为但是有NSNumber对象。所以我要循环…
EDIT II
编辑二世
mbi pointed out in the comments that there is a difference between iOS versions. So here is an iOS9 test:
mbi在评论中指出,iOS版本之间存在差异。这是iOS9测试:
NSDictionary *data = @{
@"a": @(YES),
@"b": @YES,
@"c": @(1),
@"d": @1
};
NSLog(@"%@", [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:data options:0 error:nil] encoding:NSUTF8StringEncoding]);
2016-07-05 02:23:43.964 Test App[24581:6231996] {"a":true,"b":true,"c":1,"d":1}
#3
12
Just ran across this myself, not sure if this is the best answer but...
我自己也遇到过,不知道这是不是最好的答案,但是……
Make sure to use @YES or @NO, then your outputted json will have true / false in it:
确保使用@YES或@NO,那么输出的json将包含true / false:
[NSJSONSerialization dataWithJSONObject:@{@"test": @YES} options:0 error: nil];
So you will have to turn your other "booleans" / boolean like values -> @YES / @NO when putting into the dictionary for dataWithJSONObject.
因此,当您将数据绑定到dataWithJSONObject的字典中时,您将不得不将另一个“布尔值”/布尔值—> @YES / @NO。
[NSJSONSerialization dataWithJSONObject:@{@"test": (boolLikeValue ? @YES : @NO)} options:0 error: nil];
#4
3
Yes, it is possible to output a boolean (true/false) with NSJSONSerialization by using kCFBooleanTrue and kCFBooleanFalse :
是的,可以使用kCFBooleanTrue和kCFBooleanFalse输出带有NSJSONSerialization的布尔(true/false):
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:kCFBooleanTrue, @"key_1",
kCFBooleanFalse, @"key_2",
nil]
then
然后
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:&error];
#5
1
I just hit the issue on iOS9. My case is that I have a CoreData property workout.private
which is bool mapped to NSNumber* due to CoreData handling.
我刚刚谈到iOS9。我的情况是我有一个CoreData属性测试。private,由于CoreData处理而映射到NSNumber*的bool。
When creating JSON [NSNumber numberWithBool:workout.private.boolValue]
sets expected true/false in JSON, but just workout.private
or @(workout.private.boolValue) sets "1" or "0".
创建JSON时[NSNumber withbool:workout.private]。在JSON中设置期望的真/假,但只是测试。private或@(workout.privat . boolvalue)设置“1”或“0”。
#6
0
I ran into similar issue when using CoreData's boolean, which is stored as NSNumber too. The easiest solution for me was using @():
我在使用CoreData的boolean时遇到了类似的问题,它也存储为NSNumber。对我来说最简单的解决方法是使用@():
[NSJSONSerialization dataWithJSONObject:@{@"bool": @([object.value boolValue])} options:0 error: nil];
I guess @() does recognize the BOOL value and initialize the NSNumber with numberWithBool: which leads to true/false in JSON
我猜@()确实识别BOOL值并用numberWithBool:初始化NSNumber,这导致JSON中的true/false
#1
3
No, the Foundation Object for a Bool is NSNumber
numberWithBool
, which becomes either 0 or 1. We have no Bool
Object. Same go for reading JSON
. True/false will become a NSNumber
again.
不,Bool的基对象是NSNumber withbool,它要么是0,要么是1。我们没有Bool对象。读取JSON也是一样。True/false将再次成为NSNumber。
You could create an Bool
Class and build your own parser. Arrays are Arrays and JSON
Objects are NSDictionary
. You can query the keys, test what Class lies behind and build the JSON
String from this.
您可以创建一个Bool类并构建自己的解析器。数组是数组,JSON对象是NSDictionary。您可以查询键,测试后面的类,并从中构建JSON字符串。
#2
22
When I create [NSNumber numberWithBool:NO]
, the NSJSONSerialization returns the word "false" in the JSON string.
当我创建[NSNumber withbool:NO]时,NSJSONSerialization返回JSON字符串中的单词“false”。
EDIT With the new shortcuts you can also use these handy guys:
使用新的快捷方式进行编辑,你也可以使用这些方便的工具:
@(YES) / @(NO)
@(1) / @(0)
@YES / @NO
@1 / @0
This way you can avoid something like looping through your values. I want the exact opposite behavior but have NSNumber
objects. So I have to loop...
通过这种方式,您可以避免像循环遍历您的值这样的事情。我想要完全相反的行为但是有NSNumber对象。所以我要循环…
EDIT II
编辑二世
mbi pointed out in the comments that there is a difference between iOS versions. So here is an iOS9 test:
mbi在评论中指出,iOS版本之间存在差异。这是iOS9测试:
NSDictionary *data = @{
@"a": @(YES),
@"b": @YES,
@"c": @(1),
@"d": @1
};
NSLog(@"%@", [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:data options:0 error:nil] encoding:NSUTF8StringEncoding]);
2016-07-05 02:23:43.964 Test App[24581:6231996] {"a":true,"b":true,"c":1,"d":1}
#3
12
Just ran across this myself, not sure if this is the best answer but...
我自己也遇到过,不知道这是不是最好的答案,但是……
Make sure to use @YES or @NO, then your outputted json will have true / false in it:
确保使用@YES或@NO,那么输出的json将包含true / false:
[NSJSONSerialization dataWithJSONObject:@{@"test": @YES} options:0 error: nil];
So you will have to turn your other "booleans" / boolean like values -> @YES / @NO when putting into the dictionary for dataWithJSONObject.
因此,当您将数据绑定到dataWithJSONObject的字典中时,您将不得不将另一个“布尔值”/布尔值—> @YES / @NO。
[NSJSONSerialization dataWithJSONObject:@{@"test": (boolLikeValue ? @YES : @NO)} options:0 error: nil];
#4
3
Yes, it is possible to output a boolean (true/false) with NSJSONSerialization by using kCFBooleanTrue and kCFBooleanFalse :
是的,可以使用kCFBooleanTrue和kCFBooleanFalse输出带有NSJSONSerialization的布尔(true/false):
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:kCFBooleanTrue, @"key_1",
kCFBooleanFalse, @"key_2",
nil]
then
然后
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:&error];
#5
1
I just hit the issue on iOS9. My case is that I have a CoreData property workout.private
which is bool mapped to NSNumber* due to CoreData handling.
我刚刚谈到iOS9。我的情况是我有一个CoreData属性测试。private,由于CoreData处理而映射到NSNumber*的bool。
When creating JSON [NSNumber numberWithBool:workout.private.boolValue]
sets expected true/false in JSON, but just workout.private
or @(workout.private.boolValue) sets "1" or "0".
创建JSON时[NSNumber withbool:workout.private]。在JSON中设置期望的真/假,但只是测试。private或@(workout.privat . boolvalue)设置“1”或“0”。
#6
0
I ran into similar issue when using CoreData's boolean, which is stored as NSNumber too. The easiest solution for me was using @():
我在使用CoreData的boolean时遇到了类似的问题,它也存储为NSNumber。对我来说最简单的解决方法是使用@():
[NSJSONSerialization dataWithJSONObject:@{@"bool": @([object.value boolValue])} options:0 error: nil];
I guess @() does recognize the BOOL value and initialize the NSNumber with numberWithBool: which leads to true/false in JSON
我猜@()确实识别BOOL值并用numberWithBool:初始化NSNumber,这导致JSON中的true/false