菜鸟教程小白 发表于 2022-12-13 14:53:37

ios - 使用 NSJSON 将字符串转换为字典


                                            <p><p>如何将字符串转换为字典。我们正在使用 <code>NSJSON</code> 但得到 <code>nil</code> 值</p>

<pre><code> NSString *decryptstr = ;
</code></pre>

<p>decryptstr 包含以下数据</p>

<pre><code>    {
    &#34;status&#34;:&#34;200&#34;,&#34;description&#34;:&#34;OK&#34;,&#34;count&#34;:&#34;4&#34;,&#34;reg_flag&#34;:1,&#34;cars&#34;:

[],&#34;appointments_count&#34;:&#34;0&#34;,&#34;addbymecars&#34;:[{&#34;gallery&#34;:

[],&#34;imagescount&#34;:0,&#34;id&#34;:&#34;1924453&#34;,&#34;mypage_list_flag&#34;:0,&#34;appointment_date&#34;:&#34;&#34;,
&#34;listing_status&#34;:&#34;Active&#34;,&#34;sellername&#34;:&#34;test &#34;,&#34;sellernumber&#34;:&#34;+918888888888&#34;,&#34;email&#34;:&#34;[email protected]&#34;,&#34;make&#34;:&#34;Hyundai&#34;,&#34;model&#34;:&#34;i20&#34;,&#34;model_type&#34;:&#34;Asta 1.4 CRDI&#34;,&#34;listing_type&#34;:&#34;&#34;,&#34;listing_sorting_order&#34;:&#34;&#34;,&#34;band_color&#34;:&#34;&#34;,&#34;price&#34;:&#34;65,59,595\/-&#34;,&#34;state&#34;:&#34;Telangana&#34;,&#34;city&#34;:&#34;Hyderabad&#34;,&#34;area&#34;:&#34;&#34;,&#34;mfgyear&#34;:&#34;2013&#34;,&#34;color&#34;:&#34;Green&#34;,&#34;owner&#34;:&#34;2&#34;,&#34;user_type&#34;:&#34;person&#34;,&#34;dealer_id&#34;:&#34;1503181&#34;,&#34;isfeatured&#34;:&#34;0&#34;,&#34;fueltype&#34;:&#34;Diesel&#34;,&#34;pincode&#34;:&#34;0&#34;,&#34;mileage&#34;:&#34;89,89,898&#34;,&#34;app_latitude&#34;:&#34;&#34;,&#34;app_longitude&#34;:&#34;&#34;,&#34;certification&#34;:&#34;n&#34;,&#34;certification_text&#34;:&#34;&#34;,&#34;images&#34;:{&#34;ldpi&#34;:&#34;http:\/\/imagecdn.cartrade.com\/notavailable80x60.jpg&#34;,&#34;mdpi&#34;:
&#34;http:\/\/imagecdn.cartrade.com\/notavailable100x75.jpg&#34;,&#34;hdpi&#34;:&#34;http:\/\/imagecdn.cartrade.com\/notavailable150x113.jpg&#34;,&#34;xhdpi&#34;:&#34;http:\/\/imagecdn.cartrade.com\/notavailable200x150.jpg&#34;,&#34;xxhdpi&#34;:&#34;http:\/\/imagecdn.cartrade.com\/notavailable300x225.jpg&#34;},&#34;usedcarshortlist&#34;:&#34;0&#34;,&#34;postingdate&#34;:&#34;04-Jan-2016&#34;,&#34;mobile_link&#34;:&#34;http:\/\/testm.cartrade.com\/L1924453&#34;,&#34;transmission&#34;:
&#34;Manual&#34;},{&#34;gallery&#34;:[],&#34;imagescount&#34;:0,&#34;id&#34;:&#34;1924742&#34;,&#34;mypage_list_flag&#34;:0,&#34;appointment_date&#34;:&#34;&#34;,&#34;listing_status&#34;:&#34;Active&#34;,&#34;sellername&#34;:&#34;yuyu &#34;,&#34;sellernumber&#34;:&#34;+919898889989&#34;,&#34;email&#34;:&#34;[email protected]&#34;,&#34;make&#34;:&#34;Maruti Suzuki&#34;,&#34;model&#34;:&#34;800&#34;,&#34;model_type&#34;:&#34;AC&#34;,&#34;listing_type&#34;:&#34;&#34;,&#34;listing_sorting_order&#34;:&#34;&#34;,&#34;band_color&#34;:&#34;&#34;,&#34;price&#34;:&#34;15,64,654\/-&#34;,&#34;state&#34;:&#34;Delhi&#34;,&#34;city&#34;:&#34;New Delhi&#34;,&#34;area&#34;:&#34;&#34;,&#34;mfgyear&#34;:&#34;2014&#34;,&#34;color&#34;:&#34;&#34;,&#34;owner&#34;:&#34;1&#34;,&#34;user_type&#34;:&#34;person&#34;,&#34;dealer_id&#34;:&#34;1484907&#34;,&#34;isfeatured&#34;:&#34;0&#34;,&#34;fueltype&#34;:&#34;Petrol&#34;,&#34;pincode&#34;:&#34;110001&#34;,&#34;mileage&#34;:&#34;1,233&#34;,&#34;app_latitude&#34;:&#34;28.6182&#34;,&#34;app_longitude&#34;:&#34;77.2233&#34;,
&#34;certification&#34;:&#34;n&#34;,&#34;certification_text&#34;:&#34;&#34;,&#34;images&#34;:{&#34;ldpi&#34;:&#34;http:\/\/imagecdn.cartrade.com\/notavailable80x60.jpg&#34;,&#34;mdpi&#34;:&#34;http:\/\/imagecdn.cartrade.com\/notavailable100x75.jpg&#34;,&#34;hdpi&#34;:&#34;http:\/\/imagecdn.cartrade.com\/notavailable150x113.jpg&#34;,&#34;xhdpi&#34;:&#34;http:\/\/imagecdn.cartrade.com\/notavailable200x150.jpg&#34;,&#34;xxhdpi&#34;:&#34;http:\/\/imagecdn.cartrade.com\/notavailable300x225.jpg&#34;},&#34;usedcarshortlist&#34;:&#34;0&#34;,&#34;postingdate&#34;:&#34;05-Feb-2016&#34;,&#34;mobile_link&#34;:&#34;http:\/\/testm.cartrade.com\/L1924742&#34;,&#34;transmission&#34;:&#34;Manual&#34;},{&#34;gallery&#34;:[],&#34;imagescount&#34;:0,&#34;id&#34;:&#34;1924641&#34;,&#34;mypage_list_flag&#34;:0,&#34;appointment_date&#34;:&#34;&#34;,&#34;listing_status&#34;:&#34;Active&#34;,&#34;sellername&#34;:&#34;vcnvmcx &#34;,&#34;sellernumber&#34;:&#34;+919812345687&#34;,&#34;email&#34;:&#34;[email protected]&#34;,&#34;make&#34;:&#34;Maruti Suzuki&#34;,&#34;model&#34;:&#34;Alto 800&#34;,&#34;model_type&#34;:&#34;LX&#34;,&#34;listing_type&#34;:&#34;&#34;,&#34;listing_sorting_order&#34;:&#34;&#34;,
&#34;band_color&#34;:&#34;&#34;,&#34;price&#34;:&#34;2,50,000\/-&#34;,&#34;state&#34;:&#34;Maharashtra&#34;,&#34;city&#34;:&#34;Mumbai&#34;,&#34;area&#34;:&#34;New Hyderabad&#34;,&#34;mfgyear&#34;:&#34;2015&#34;,&#34;color&#34;:&#34;&#34;,&#34;owner&#34;:&#34;1&#34;,&#34;user_type&#34;:&#34;person&#34;,&#34;dealer_id&#34;:&#34;1503221&#34;,&#34;isfeatured&#34;:&#34;0&#34;,&#34;fueltype&#34;:&#34;Petrol&#34;,&#34;pincode&#34;:&#34;500001&#34;,&#34;mileage&#34;:&#34;50,000&#34;,&#34;app_latitude&#34;:&#34;17.3897&#34;,&#34;app_longitude&#34;:&#34;78.478&#34;,&#34;certification&#34;:&#34;n&#34;,&#34;certification_text&#34;:&#34;&#34;,&#34;images&#34;:{&#34;ldpi&#34;:&#34;http:\/\/imagecdn.cartrade.com\/notavailable80x60.jpg&#34;,&#34;mdpi&#34;:&#34;http:\/\/imagecdn.cartrade.com\/notavailable100x75.jpg&#34;,&#34;hdpi&#34;:&#34;http:\/\/imagecdn.cartrade.com\/notavailable150x113.jpg&#34;,&#34;xhdpi&#34;:&#34;http:\/\/imagecdn.cartrade.com\/notavailable200x150.jpg&#34;,&#34;xxhdpi&#34;:&#34;http:\/\/imagecdn.cartrade.com\/notavailable300x225.jpg&#34;},
&#34;usedcarshortlist&#34;:&#34;1&#34;,&#34;postingdate&#34;:&#34;03-Feb-2016&#34;,&#34;mobile_link&#34;:&#34;http:\/\/testm.cartrade.com\/L1924641&#34;,&#34;transmission&#34;:&#34;Manual&#34;
    }
    ]}
</code></pre>

<p>使用 <code>NSJSON</code></p>

<pre><code> NSError *error;

NSData *jsonData = ;
NSDictionary *returndict = [NSJSONSerialization JSONObjectWithData:jsonData
                                                         options:NSJSONReadingMutableContainers
                                                             error:&amp;error];
</code></pre>

<p>returndict 得到 nil 值。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在转换你的字符串之前添加这一行</p>

<pre><code>NSString *decryptstr = ;
decryptstr = ];
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 使用 NSJSON 将字符串转换为字典,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/35593952/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/35593952/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 使用 NSJSON 将字符串转换为字典