简体   繁体   English

通过JSON将NSDictionary / NSArray中的KEYS保持正确的ORDER

[英]Keeping KEYS in correct ORDER in NSDictionary/NSArray from JSON

I have a JSON array returning from a request. 我有一个从请求返回的JSON数组。
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"}
}]

I receive the JSON array in this order. 我按此顺序接收JSON数组。

I have the below Objective-C: 我有以下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. 我读过字典没有按定义排序,因此很高兴使用数组而不是字典。

But I have seen SEVERAL threads on StackOverflow regarding possible solution (using keysSortedByValueUsingSelector ): 但是我已经在StackOverflow上看到了几个关于可能解决方案的线程(使用keysSortedByValueUsingSelector ):
Getting NSDictionary keys sorted by their respective values 获取NSDictionary键按其各自的值排序
Can i sort the NSDictionary on basis of key in Objective-C? 我可以根据Objective-C中的密钥对NSDictionary进行排序吗?
sort NSDictionary keys by dictionary value into an NSArray 将NSDictionary键按字典值排序到NSArray中
Can i sort the NSDictionary on basis of key in Objective-C? 我可以根据Objective-C中的密钥对NSDictionary进行排序吗?

I have tried them and am getting nowhere. 我已经尝试过了,却一无所获。 Not sure if it is just my implementation which is wrong. 不知道这是否只是我的实现错误。

I tried (attempt 1): 我尝试了(尝试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);

            }
}

Receive error: [__NSArrayM keySortedByValueUsingComparator:]: unrecognized selector sent to instance . 接收错误: [__NSArrayM keySortedByValueUsingComparator:]: unrecognized selector sent to instance

Also tried (attempt 2): 也尝试过(尝试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
}

But another error on selector even though clients is a NSMutableDictionary. 但是即使客户端是NSMutableDictionary,选择器上的另一个错误。

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

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. 我在哪里,我认为我会遍历点NSMutableDictionary ,并手动将它们放入一个新NSMutableArray以正确的顺序。

Any ideas would be very much appreciated? 任何想法将不胜感激?
OR anybodies code snippet attempt at my problem would be equally appreciated. 或任何针对我的问题的代码片段尝试均将受到赞赏。

Not only is NSDictionary in principle unordered, but so are JSON objects. NSDictionary原则上不仅是无序的,而且JSON对象也是如此。 These JSON objects are identical : 这些JSON对象是相同的

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

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. 当然,您可以只使用keysSortedByValueUsingComparator:方法来获得已排序的键。

First, the exception you're seeing is because the selector name is incorrect. 首先,您看到的异常是因为选择器名称不正确。 You should use keysSortedByValueUsingComparator . 您应该使用keysSortedByValueUsingComparator Second, you can't sort the keys, because the order in which you're getting them from the server doesn't seem to be in any particular order. 其次,您无法对键进行排序,因为从服务器获取键的顺序似乎没有特定的顺序。 If you MUST have them in that order, then send them from the server with a tag ("index") and then sort by this tag. 如果必须按顺序排列它们,则使用标签(“ index”)从服务器发送它们,然后按此标签排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM