ios - Web服务响应字典禁用按键自动排序?
<p><p>我的网络服务响应存在问题。字典键是自动排序的,我想要它们原样</p>
<p>实际的网络服务响应是:- </p>
<pre><code> => Array
(
=> How it works
=> Benefits
=> Win Free Airtime
=> What can I Report?
=> Our Goal
=> Disclaimer
=> FAQ
=> Terms & Conditions
=> Contact Us
=> Feedback / suggestion
)
</code></pre>
<p>不幸的是,它通过自动排序显示在下面</p>
<pre><code>data = {
10 = "Our Goal";
11 = "Contact Us";
13 = "Terms & Conditions";
14 = "Feedback / suggestion";
16 = Disclaimer;
18 = "How it works";
22 = Benefits;
23 = "Win Free Airtime";
7 = "What can I Report?";
8 = FAQ;
};
</code></pre>
<p>我被卡住了:(下面是我的代码</p>
<pre><code>[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
;
if ( > 0 && error == nil)
{
//NSString* newStr = [ initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary * Dict = ;
NSString* newStr = [ initWithData:data encoding:NSUTF8StringEncoding];
NSData *decryptedStr = [ initWithBase64EncodedString:newStr options:0];
NSDictionary * Dict=;
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>没有办法做到这一点,因为结果不是一个数组而是一个没有顺序的字典。</p>
<p>您必须编写自己的 JSON 解析器并使用 <code>NSDictionary</code> 以外的其他东西来保持顺序。</p>
<p>最好的方法是使用数组来改变 JSON 输出:</p>
<pre><code>{
"data": [
{
"key": 10,
"value": "Our Goal"
},
{
"key": 11,
"value": "Contact Us"
},
{
"key": 13,
"value": "Terms & Conditions"
},
{
"key": 14,
"value": "Feedback / suggestion"
},
{
"key": 16,
"value": "Disclaimer"
},
{
"key": 18,
"value": "How it works"
},
{
"key": 22,
"value": "Benefits"
},
{
"key": 23,
"value": "Win Free Airtime"
},
{
"key": 7,
"value": "What can I Report?"
},
{
"key": 8,
"value": "FAQ"
}
]
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - Web服务响应字典禁用按键自动排序?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/25443878/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/25443878/
</a>
</p>
页:
[1]