在JDS的NSDictionary / NSArray中保持KEYS的正确ORDER [英] Keeping KEYS in correct ORDER in NSDictionary/NSArray from JSON

查看:98
本文介绍了在JDS的NSDictionary / NSArray中保持KEYS的正确ORDER的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从请求返回的JSON数组。

数据是这样的(它来自外部源,无法从服务器更改):

I have a JSON array returning from a request.
Data is as such (it comes from external source and cannot be changed from server):

[{"clients":
{"name":"Client name here","telephone":"00000","email":"clientemail@hotmail.com","website":"www.clientname.com"}
}]

我按此顺序收到JSON数组。

I receive the JSON array in this order.

我有以下Objective-C:

I have the below Objective-C:

//jsonString is the json string inside type NSString.
NSMutableDictionary *tempDict = [jsonString JSONValue];
NSMutableDictionary *clients = [tempDict objectForKey:@"clients"];

for(NSString *key in clients) {
        id value = [tempDict objectForKey:key]; 
            for(NSString *itemKey in key) {
                NSLog(@"ITEM KEY %@: ",itemKey);

            }
}

打印密钥的顺序是:

电话

电子邮件

名称

网站

The order it prints keys is:
telephone
email
name
web site

我需要它来打印如何收到(例如):

名称

电话

电子邮件

网站

I need it to print how it is received (eg):
name
telephone
email
web site

我已经读过字典按照定义没有排序,所以很高兴使用数组而不是字典。

I have read that Dictionaries are unsorted by definition so am happy to use an Array instead of a dictionary.

但我在StackOverflow上看到了关于可能解决方案的几个线程(使用 keysSortedByValueUsingSelector ):

获取NSDictionary键按各自的值排序

我可以对NSDictionary进行排序Objective-C中键的基础?

按字典值将NSDictionary键排序到NSArray中

我可以在此基础上对NSDictionary进行排序Objective-C中的密钥是什么?

But I have seen SEVERAL threads on StackOverflow regarding possible solution (using keysSortedByValueUsingSelector):
Getting NSDictionary keys sorted by their respective values
Can i sort the NSDictionary on basis of key in Objective-C?
sort NSDictionary keys by dictionary value into an NSArray
Can i sort the NSDictionary on basis of key in Objective-C?

我已经尝试过它们并且无处可去。不确定这只是我的实现是错误的。

I have tried them and am getting nowhere. Not sure if it is just my implementation which is wrong.

我试过(尝试1):

NSArray *orderedKeys = [clients keySortedByValueUsingComparator:^NSComparisonResult(id obj1, id obj2){
    return [obj1 compare:obj2];
}];

for(NSString *key in orderedKeys) {
        id value = [tempDict objectForKey:key]; 
            for(NSString *keyThree in key) {
                NSLog(@"API-KEY-ORDER %@: ",keyThree);

            }
}

收到错误: [__ NSArrayM keySortedByValueUsingComparator:]:无法识别的选择器发送到实例

Receive error: [__NSArrayM keySortedByValueUsingComparator:]: unrecognized selector sent to instance.

也试过(尝试2):

NSArray *sortedKeys = [[clients allKeys] sortedArrayUsingSelector: @selector(compare:)];
NSMutableArray *sortedValues = [NSMutableArray array];
for (NSString *key in sortedKeys){
    [sortedValues addObject: [tempDict objectForKey: key]];
    //[sortedValues addObject: [all objectForKey: key]]; //failed
}

但是,即使客户端是NSMutableDictionary,选择器上的另一个错误。

But another error on selector even though clients is a NSMutableDictionary.

[__NSArrayM allKeys]: unrecognized selector sent to instance 0x4b6beb0

我认为我将迭代 NSMutableDictionary 并手动将它们放入一个新的 NSMutableArray 按照正确的顺序。

I am at the point where I think I am going to iterate over the NSMutableDictionary and manually place them into a new NSMutableArray in the correct order.

任何想法都会非常感激吗?

OR anybodies代码片段尝试我的问题同样会受到赞赏。

Any ideas would be very much appreciated?
OR anybodies code snippet attempt at my problem would be equally appreciated.

推荐答案

NSDictionary原则上不仅无序,但JSON对象也是如此。这些JSON对象相同

Not only is NSDictionary in principle unordered, but so are JSON objects. These JSON objects are identical:

[{"clients":
{"name":"Client name here","telephone":"00000","email":"clientemail@hotmail.com","website":"www.clientname.com"}
}]

[{"clients":
{"website":"www.clientname.com","name":"Client name here","telephone":"00000","email":"clientemail@hotmail.com"}
}]

如果服务器想要订购密钥,它需要发送一个数组。当然,您只需使用keysSortedByValueUsingComparator:方法即可按排序顺序获取密钥。

If the server wants to have ordered keys, it needs to send an array. Of course you can just use the keysSortedByValueUsingComparator: method to get the keys in a sorted order.

这篇关于在JDS的NSDictionary / NSArray中保持KEYS的正确ORDER的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆