菜鸟教程小白 发表于 2022-12-11 17:08:19

iOS - 将 JSON 字符串转换为 JSON 对象后更改的十进制值


                                            <p><p>我从后台获取数据</p>

<pre><code>    NSString * jsonStr = data[@&#34;result&#34;];
    NSData * jsonData = ;
    NSDictionary * dic = ;
</code></pre>

<p>这是 JsonStr:</p>

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

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

<p>这是字典:</p>

<pre><code>    {
brand = &#34;\U4e2d\U8054&#34;;
createTime = &#34;2016-07-20 10:58:09&#34;;
ext1 = &#34;65.90000000000001&#34;;
ext2 = &#34;34.6&#34;;
ext3 = &#34;100.6&#34;;
ext4 = 75;
ext5 = 65805;
id = 2;
isValid = 1;
name = &#34;\U8f6e\U80ce\U5f0f\U8d77\U91cd\U673a&#34;;
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(@&#34;%@&#34;, ext1);
</code></pre>

<p>将是 65.9。</p>

<p>NSString:</p>

<pre><code>NSString *ext1Str = ;
NSLog(@&#34;%@&#34;, 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]
查看完整版本: iOS - 将 JSON 字符串转换为 JSON 对象后更改的十进制值