2011-05-09 4 views
3

:SBJsonParser 아이폰 앱에서 내가 JSON 라이브러리 작업이 상황을 볼 수있어

변환 JSON 문자열 시나리오

있는 NSDictionary-1 : 나는 결과를 볼

NSString *jsonString = @"{\"Name\":\"Foo\", Points:5}"; 
NSDictionary *dictionary = (NSDictionary*)[jsonParser objectWithString:jsonString]; 
NSLog(@"Dictionary: %@",dictionary); 

을 다음과 같이 :

사전 : { Name = "Foo"; 포인트 = 5; }

맞습니다.

시나리오 2 :

NSString *jsonString = @"{\"Name\":\"Foo\", Points:0.5}"; 
NSDictionary *dictionary = (NSDictionary*)[jsonParser objectWithString:jsonString]; 
NSLog(@"Dictionary: %@",dictionary); 

I는 다음과 같이 결과 참조 :

사전 { NAME = "푸"; Points = "0.5"; } ???

시나리오 3 :

NSString *jsonString = @"{\"Name\":\"Foo\", Points:-1}"; 
NSDictionary *dictionary = (NSDictionary*)[jsonParser objectWithString:jsonString]; 
NSLog(@"Dictionary: %@",dictionary); 

I는 다음과 같이 결과 참조 :

사전 { NAME = "푸"; Points = "-1"; } ???

JSON 라이브러리가 음수 또는 1보다 작은 숫자를 문자열로 변환하는 이유는 무엇입니까? 이런 일이 발생하지 않도록하는 방법을 알고 계십니까?

답변

1

"이유"가 없지만 검색 할 때 intValue 또는 floatValue을 부를 수 있으므로 문제가되지 않을 수 있습니다.

NSLog(@"Points = %.2f", [[dictionary valueForKey:@"Points"] floatValue]); 
+0

나는 두 번째로 ... 내가하는 일이기도합니다. – Sid