OStack程序员社区-中国程序员成长平台

标题: ios - Web服务响应字典禁用按键自动排序? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 01:08
标题: ios - Web服务响应字典禁用按键自动排序?

我的网络服务响应存在问题。字典键是自动排序的,我想要它们原样

实际的网络服务响应是:-

[data] => Array
        (
            [18] => How it works
            [22] => Benefits
            [23] => Win Free Airtime
            [7] => What can I Report?
            [10] => Our Goal
            [16] => Disclaimer
            [8] => FAQ
            [13] => Terms & Conditions
            [11] => Contact Us
            [14] => Feedback / suggestion
        )

不幸的是,它通过自动排序显示在下面

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

我被卡住了下面是我的代码

[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
     {


         [spinner stopAnimating];
         if ([data length] > 0 && error == nil)
         {
             //NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

             NSDictionary * Dict = [NSDictionary dictionary];
             NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSData *decryptedStr = [[NSData alloc] initWithBase64EncodedString:newStr options:0];

NSDictionary * Dict  =  [NSJSONSerialization JSONObjectWithData:decryptedStr options:kNilOptions error:nil];



Best Answer-推荐答案


没有办法做到这一点,因为结果不是一个数组而是一个没有顺序的字典。

您必须编写自己的 JSON 解析器并使用 NSDictionary 以外的其他东西来保持顺序。

最好的方法是使用数组来改变 JSON 输出:

{
    "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"
        }
    ]
}

关于ios - Web服务响应字典禁用按键自动排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25443878/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4