开发者

SBJsonParser in iPhone app

开发者 https://www.devze.com 2023-03-04 06:23 出处:网络
I\'m working with JSON library and see this situation: Convert JSON string to NSDictionary Scenario 1: NSString *jsonString = @\"{\\\"Name\\\":\\\"Foo\\\", Points:5}\";

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]);
0

精彩评论

暂无评论...
验证码 换一张
取 消