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

目标-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"]);

10-04 19:41