3

I am using AFNetworking to retrieve JSON data from a web service. Part of the response string I get is:

{"DATA":{"LEASE TYPE":"3 Yrs + 0 renew of 0 Yrs","LANDLORD":"","TENANT":"test comp"... 

and so on. The order of the key values in the "DATA" dictionary ("LEASE TYPE","LANDLORD","TENANT"...) is important for presentation purposes. However, when AFNetworking calls NSJSONSerialization's:

+ (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error;

method, the returned dictionary has the keys in a different order.

I notice that the AFJSONRequestOperation object has the server's response stored as an NSString, with everything in the correct order. However I'm not keen on parsing the JSON by hand if I can avoid it.

Can anyone suggest a way that will let me get at / keep the keys in their original order?

Thanks.

4

3 回答 3

4

如果顺序很重要,请使用数组而不是字典,字典本质上是无序的。或按所需顺序添加字典键数组。

如果您无法控制发送的响应,则至少必须自己解析 JSON 以进行排序。

于 2013-04-18T11:23:18.957 回答
0

当你创建一个 NSDictionary 时,顺序会不一样。我经常认识到字典是按键名字母顺序排列的。

但是当使用字典时,顺序并不重要。他们不应该!

于 2013-04-18T11:08:18.123 回答
0

正如前面的答案提到的字典本质上是没有顺序的,但你可以在这里找到一个很好的OrderedDictionary类:

http://www.cocoawithlove.com/2008/12/ordereddictionary-subclassing-cocoa.html

http://projectswithlove.com/projects/OrderedDictionary.zip

于 2013-04-18T21:20:48.243 回答