SBJsonParser in iPhone app
I'm working with JSON library and see this situation:
Convert JSON string to NSDictionary
Scenario 1:
NSString *jsonString = @"{\"Name\":\"Foo\", Points:5}";
NSDictionary *dictionary = (NSDictionary*)[jsonParser objectWithString:jsonString];
NSLog(@"Dictionary: %@",dictionary);
I see the result as followed:
Dictionary: { Name = "Foo"; Points = 5; }
So that's correct.
Scenario 2:
NSString *jsonString = @"{\"Name\":\"Foo\", Points:0.5}";
NSDictionary *dictionary = (NSDictionary*)[jsonParser objectWithString:jsonString];
NSLog(@"Dictionary: %@",dictionary);
I see the result as followed:
Dictionary: { Name = "Foo"; Points = "0.5"; } ???
Scenario 3:
NSString 开发者_如何学C*jsonString = @"{\"Name\":\"Foo\", Points:-1}";
NSDictionary *dictionary = (NSDictionary*)[jsonParser objectWithString:jsonString];
NSLog(@"Dictionary: %@",dictionary);
I see the result as followed:
Dictionary: { Name = "Foo"; Points = "-1"; } ???
Why does the JSON library convert the negative numbers or number less than 1 into string? Do you know how to prevent this from happening?
I do not have the "why", but it may not be an issue for you since you can call for the intValue
or floatValue
when retrieving.
NSLog(@"Points = %.2f", [[dictionary valueForKey:@"Points"] floatValue]);
精彩评论