iOS - 将 JSON 字符串转换为 JSON 对象后更改的十进制值
<p><p>我从后台获取数据</p>
<pre><code> NSString * jsonStr = data[@"result"];
NSData * jsonData = ;
NSDictionary * dic = ;
</code></pre>
<p>这是 JsonStr:</p>
<pre><code> {"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"}
</code></pre>
<p>在 JsonStr 中,@"ext1"的值为 65.9,但在 jsonobject 之后,该值变为 '65.90000000000001'</p>
<p>这是字典:</p>
<pre><code> {
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;
</code></pre>
<p>}</p>
<p>这些数据有什么问题?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>当你使用 <code>ext1</code> 作为 NSNumber 时:</p>
<pre><code>NSNumber *ext1 = @( floatValue]);
NSLog(@"%@", ext1);
</code></pre>
<p>将是 65.9。</p>
<p>NSString:</p>
<pre><code>NSString *ext1Str = ;
NSLog(@"%@", ext1Str);
</code></pre>
<p><code>2016-08-01 13:19:46.969 OCDemo 65.9
2016-08-01 13:19:46.970 OCDemo 65.9</code></p></p>
<p style="font-size: 20px;">关于iOS - 将 JSON 字符串转换为 JSON 对象后更改的十进制值,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/38691239/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/38691239/
</a>
</p>
页:
[1]