gpt4 book ai didi

iOS - 将 JSON 字符串转换为 JSON 对象后十进制值发生变化

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:58:42 24 4
gpt4 key购买 nike

我从后台获取数据

    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'

这是 dic:

    {
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;

这个数据有什么问题?

最佳答案

当你使用 ext1 作为 NSNumber 时:

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

它将是 65.9。

NS字符串:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com