在Objective C中按顺序保持JSON对象的字典键 [英] Keeping dictionary keys of JSON Object in order in Objective C

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

问题描述

我在目标C中使用JSON-FRAMEWORK来解析JSON对象. 当我调用[jsonString JSONValue]时,我得到了一个字典,但是键的顺序与我解析的JSON对象的顺序不同.无论如何,要保持此顺序不变?

I'm using the JSON-FRAMEWORK in objective C to parse a JSON object. When I call [jsonString JSONValue], I get back a dictionary, but the keys aren't in the same order as the JSON Object I'm parsing. Is there anyway to keep this order the same?

推荐答案

在JSON对象中,根据定义,键值对的顺序没有意义.该规范允许JSON生产者以其想要的任何方式置换它们,甚至是随机的-并且不需要解析器来保留顺序. RFC 4627说:

In JSON objects, by definition, the order of the key-value pairs is not meaningful. The specification allows a JSON producer to permute them any way it want, even at random -- and does not require a parser to preserve the ordering. RFC 4627 says:

对象是零个或多个名称/值的无序集合 对,其中名称是字符串,值是字符串,数字, 布尔值,null,对象或数组.

An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.

如果需要保留顺序,那么您拥有的不是JSON,而是一种家庭定义的格式,恰好看起来像JSON.

If you need the ordering to be preserved, what you have is not JSON, but a home-defined format that just happens to look superficially like JSON.

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

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