OStack程序员社区-中国程序员成长平台

标题: iOS - 将 JSON 字符串转换为 JSON 对象后更改的十进制值 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:08
标题: iOS - 将 JSON 字符串转换为 JSON 对象后更改的十进制值

我从后台获取数据

    NSString * jsonStr = data[@"result"];
    NSData * jsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];

这是 JsonStr:

    {"id":2,"name":"轮胎式起重机","brand":"中联","type":"QY130","weight":130.0,"isValid":"1","ext1":65.9,"ext2":34.6,"ext3":100.6,"ext4":75.0,"ext5":65805.0,"createTime":"2016-07-20 10:58:09"}

在 JsonStr 中,@"ext1"的值为 65.9,但在 jsonobject 之后,该值变为 '65.90000000000001'

这是字典:

    {
brand = "\U4e2d\U8054";
createTime = "2016-07-20 10:58:09";
ext1 = "65.90000000000001";
ext2 = "34.6";
ext3 = "100.6";
ext4 = 75;
ext5 = 65805;
id = 2;
isValid = 1;
name = "\U8f6e\U80ce\U5f0f\U8d77\U91cd\U673a";
type = QY130;
weight = 130;

}

这些数据有什么问题?



Best Answer-推荐答案


当你使用 ext1 作为 NSNumber 时:

NSNumber *ext1 = @([dic[@"ext1"] floatValue]);
NSLog(@"%@", ext1);

将是 65.9。

NSString:

NSString *ext1Str = [NSString stringWithFormat"%@",ext1];
NSLog(@"%@", ext1Str);

2016-08-01 13:19:46.969 OCDemo[2200:78063] 65.9 2016-08-01 13:19:46.970 OCDemo[2200:78063] 65.9

关于iOS - 将 JSON 字符串转换为 JSON 对象后更改的十进制值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38691239/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4