简体   繁体   English

在Objective C中按顺序保存JSON Object的字典键

[英]Keeping dictionary keys of JSON Object in order in Objective C

I'm using the JSON-FRAMEWORK in objective C to parse a JSON object. 我在目标C中使用JSON-FRAMEWORK来解析JSON对象。 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. 当我调用[jsonString JSONValue]时,我得到一个字典,但是键与我正在解析的JSON对象的顺序不同。 Is there anyway to keep this order the same? 无论如何要保持这个顺序相同吗?

In JSON objects, by definition , the order of the key-value pairs is not meaningful. 在JSON对象中, 根据定义 ,键值对的顺序没有意义。 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. 该规范允许JSON生成器以任何方式置换它们,即使是随机的 - 并且不需要解析器来保留排序。 RFC 4627 says: RFC 4627说:

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. 对象是零个或多个名称/值对的无序集合,其中名称是字符串,值是字符串,数字,布尔值,空值,对象或数组。

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. 如果您需要保留订单,那么您拥有的不是JSON,而是一种家庭定义的格式,恰好看起来像JSON。

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

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