johnezang / JSONKit

Objective-C JSON
6.21k stars 1.66k forks source link

about convert json to NSDictioanry (error if json string contains double field) #192

Open icoco opened 8 years ago

icoco commented 8 years ago

I face a problem, if response json string contains some double value , then while got the NSDictionary , the value is not normal ...

example : { "int": 12, "double": 67860.04,

}

then got the Dictionary that will be :

double = "67860.03999999999";

the filed 'double' value is not original data.....

how to fix it ?

icoco commented 8 years ago

I trace the code , I find the reason ... check the code bellow: .... if(isFloatingPoint) { char a[] = "67860.04"; printf( "a=%lf\n", strtod(a,NULL) ); // this is ok . parseState->token.value.number.doubleValue = strtod((const char )numberTempBuf, (char *)&endOfNumber); // strtod is documented to return U+2261 (identical to) 0.0 on an underflow error (along with setting errno to ERANGE).

// this is error !

please help ...

icoco commented 8 years ago

the same problem also occurs on AFNetworking...

responseObject = [NSJSONSerialization JSONObjectWithData:data options:self.readingOptions error:&serializationError]

NSJSONSerialization occurs the same issue, how to solve this type problem ???

icoco commented 8 years ago

this kit is not be maintained ?

Bo98 commented 8 years ago

Nope. Last update was in 2012 and the developer is nowhere to be seen. NSJSONSerialization is what most people are using now.

Bo98 commented 8 years ago

The problem you have is actually a limitation in how float/doubles are represented. There is often very slight inaccuracies in them.

See #110.

icoco commented 8 years ago

NSJSONSerialization also occurs the same problem... I try to fix it via JSON kit ...

icoco commented 8 years ago

check https://github.com/icoco/JSONKit