ios - 我可以通过 NSJSONSerialization 序列化负整数吗?

标签 ios nsnumber nsjsonserialization

我无法通过 NSJSONSerialization 序列化负整数。

objective-C :

NSMutableDictionary *sendData = [NSMutableDictionary dictionaryWithObjectsAndKeys: 
                          @480, @"width",
                          @800, @"height"
                          @-10, @"value1", 
                          @10, @"value2", nil];


NSError *error = nil;
NSData *dataJSon = [NSJSONSerialization dataWithJSONObject:sendData options:0 error:&error];
NSLog(@"sendData\n %@ \n\n", [sendData description]);

日志

sendData{ 
           width = 480;      // int
           height = 800;     // int
           value1 = "-10";   // string. Not integer!!
           value2 = 10;      // int
          }

如何序列化负整数?

最佳答案

你是对的,一切都很好。这只是字典描述的误导。

NSMutableDictionary *sendData = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                     [NSNumber numberWithInt:-10], @"value1",
                                     [NSNumber numberWithInt:10], @"value2", nil];


    NSError *error = nil;
    NSData *dataJSon = [NSJSONSerialization dataWithJSONObject:sendData options:0 error:&error];
    NSLog(@"sendData\n %@ \n\n", [sendData objectForKey:@"value1"]);

关于ios - 我可以通过 NSJSONSerialization 序列化负整数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30183526/

相关文章:

ios - 按类别及其子元素填充列表

ios - 如何在 iOS 应用程序中发送文本?

ios - 为什么 NSNumber 从一个函数到另一个函数变成零?

iOS 将大数字转换为小格式

ios - NSJSON序列化和表情符号

objective-c - 对象为零,除非我添加另一行 'random' 代码

ios - 是否可以向应用程序中的所有键盘添加完成键?

swift - 在 Swift 中将整数转换为罗马数字字符串

ios - 使用 NSJSONSerialization 序列化一个 unsigned long long

Swift:无法序列化 Json 响应