简体   繁体   English

JSONKit:如何保持JSON字典键的顺序

[英]JSONKit: how to keep order of JSON dictionary keys

I've some JSON String that represents a Map (Dictionary) from the server. 我有一些JSON字符串表示来自服务器的Map(字典)。

I need to draw this map as it is @ my iPhone app. 我需要绘制这张地图,因为它是@我的iPhone应用程序。

I am using JSONKit and it seems that it uses Hashing function to insert keys into its internal JKDictionary.... 我正在使用JSONKit,它似乎使用Hashing函数将键插入其内部JKDictionary ....

So should I send a priority number to order the keys? 那么我应该发送一个优先号码来订购钥匙吗? Or there is some way to make JSONKit to preserve keys ordering of the server JSON data? 或者有一些方法可以使JSONKit保留服务器JSON数据的键排序?

You can choose to add a order property to your dictionary. 您可以选择将order属性添加到字典中。 And sort your keys using that property. 并使用该属性对您的密钥进行排序。 Then you can enumerate your dictionary using your sorted keys. 然后,您可以使用排序的键枚举字典。 enumerateKeysAndObjectsUsingBlock: enumerateKeysAndObjectsUsingBlock:

// Here I suppose you have added another number property `order` for your dictionary's values
NSArray *sortedKeys = [dic keysSortedByValueUsingComparator:^(id obj1, id obj2){
    return [obj1 order] < [obj2 order];
}];

// for in will give your an ordered travel for your sortedKeys
for (id key in sortedKeys) {
    // handle your dic
    [dic objectForKey:key];
}

Dictionaries are not ordered, but arrays are, so if you can change your JSON data structure to an array, you should get an ordered result. 字典不是有序的,但是数组是,所以如果你可以将你的JSON数据结构更改为数组,你应该得到一个有序的结果。

[{'key1':'value1'},{'key2':'value2'},{'key3':'value3'}]

What does your data look like? 你的数据是什么样的?

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

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