JSONKit:如何保持JSON字典键的顺序 [英] JSONKit: how to keep order of JSON dictionary keys

查看:309
本文介绍了JSONKit:如何保持JSON字典键的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些JSON字符串,表示来自服务器的地图(词典).

I've some JSON String that represents a Map (Dictionary) from the server.

我需要绘制这张地图,因为它是我的iPhone应用程序.

I need to draw this map as it is @ my iPhone app.

我正在使用JSONKit,似乎它使用散列函数将密钥插入其内部JKDictionary....

I am using JSONKit and it seems that it uses Hashing function to insert keys into its internal JKDictionary....

那我应该发送优先级数字来订购钥匙吗?还是有某种方法可以使JSONKit保留服务器JSON数据的键顺序?

推荐答案

您可以选择将order属性添加到字典中.然后使用该属性对密钥进行排序.然后,您可以使用已排序的键枚举字典.

You can choose to add a order property to your dictionary. And sort your keys using that property. Then you can enumerate your dictionary using your sorted keys. 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];
}

这篇关于JSONKit:如何保持JSON字典键的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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