我无法从JSON解析正确的double

时间:2015-09-18 10:49:59

标签: ios json nsjsonserialization

我对NSJSONSerialization有疑问。我有一个JSON对象的纬度和经度值(37.321398,28.292399)。

NSData *l_responseData              = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseCode error:&error];
NSMutableDictionary* l_serverResult = [NSJSONSerialization JSONObjectWithData: l_responseData options:NSJSONReadingMutableContainers error:&error];

NSData *l_responseData具有正确的数字而不会失去JSON的精确度但是当我使用NSJSONSerialization时JSONObjectWithData:l_responseData .... NSMutableDictionary * l_serverResult具有失真精度的纬度和经度值(双精度)(37.3214,28.2924)。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您可以确定l_responseData包含正确的数据吗?据我所知,JSONObjectWithData:options:error:在解析时不会丢失任何内容。

以下是一个示例代码来证明这一点:

NSMutableDictionary *test = [NSMutableDictionary dictionaryWithDictionary:@{@"Lat": @37.321398, @"Long" : @28.292399}];
NSData *data = [NSJSONSerialization dataWithJSONObject:test options:NSJSONWritingPrettyPrinted error:nil];

NSDictionary *test1 = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

NSLog(@"test1 = %@", test1); // This prints correct values.

因此,我建议您检查从服务器获得的响应。