Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 *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?

like image 556
Tuyen Nguyen Avatar asked Aug 12 '26 14:08

Tuyen Nguyen


1 Answers

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]);
like image 179
dredful Avatar answered Aug 15 '26 05:08

dredful