【问题标题】:I can't parse correct double from JSON我无法从 JSON 解析正确的双精度
【发布时间】:2015-09-18 10:49:58
【问题描述】:

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....

我该如何解决这个问题?

【问题讨论】:

    标签: ios json nsjsonserialization


    【解决方案1】:

    你能确定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.
    

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

    【讨论】:

      猜你喜欢
      • 2016-03-03
      • 2020-07-01
      • 1970-01-01
      • 2019-06-14
      • 1970-01-01
      • 1970-01-01
      • 2014-04-02
      • 2020-02-29
      • 1970-01-01
      相关资源
      最近更新 更多