如何保留通过AFNetworking检索的JSON数据的顺序? [英] How to retain order of JSON data retrieved with AFNetworking?

查看:260
本文介绍了如何保留通过AFNetworking检索的JSON数据的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AFNetworking从Web服务检索JSON数据.我得到的响应字符串的一部分是:

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"... 

,依此类推.出于演示目的,数据"字典中的键值顺序(租赁类型",地主",租户" ...)很重要.但是,当AFNetworking调用NSJSONSerialization's时:

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.

我注意到AFJSONRequestOperation对象将服务器的响应存储为NSString,所有内容均以正确的顺序排列.但是,如果可以避免的话,我并不热衷于手动解析JSON.

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?

谢谢.

推荐答案

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

If the order is important use an array not a dictionary, dictionaries are be by their nature unordered. Or add an array of dictionary keys in the order desired.

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

If you have no control over the response that is sent you will have to parse the JSON yourself at least for the ordering.

这篇关于如何保留通过AFNetworking检索的JSON数据的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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