简体   繁体   English

如何获得与目标c中的json相同的字典顺序

[英]how to get dictionary same order as same i am getting from json in objective c

i am parsing json and what i get set of dictionary but after parsing it will automatically change it order. 我正在解析json和我得到的字典集,但解析后它将自动更改其顺序。 i just need same order as same i am getting from json parsing. 我只需要从json解析中得到的相同顺序。

NOTE: i want to make one functionality which depends on dictionary order, i don't want to make it manually. 注意:我想根据字典顺序制作一种功能,我不想手动制作。 so it will not need to make it every-time to do.it will help to change dynamically in future 因此,它不需要每次都做。它将有助于未来的动态变化

Example: From Json: 示例: 从杰森:

Section:
{
  category:{},
  location:{},
  vehicle_type:{},
  mode_type:{}
}

after convert into NSDicationary: 转换成NSDicationary之后:

Section:
{
  vehicle_type:{}
  category:{},
  location:{},
  mode_type:{}

}

Thanks 谢谢

Order of key:value doesn't matter for JSON, as you can directly access value with the help of key string. key:value顺序对于JSON无关紧要,因为您可以借助密钥字符串直接访问value。 An dictionary does not support indexing of elements, so you can't. 词典不支持元素索引,因此您不能。

Dictionaries are not ordered collections. 字典不是有序集合。 On other hand, arrays are. 另一方面,数组是。

Note, if you don't have access over the json format, you got no guarantees about the order of elements, if you don't have hardcoded logic in your app. 请注意,如果您无法访问json格式,并且您的应用程序中没有硬编码逻辑,则无法保证元素的顺序。

Having said that, let's take a deeper look into your case. 话虽如此,让我们更深入地研究您的案子。 It happens that you have one main object in your json that is Section . 碰巧您在json中有一个主要对象Section What is more, you have 4 ordered properties of this object that are of specific kind of their own: 0: category; 1: location; 2: vehicle_type; 3: mode_type 而且,您有4个此对象的有序属性,它们各自具有特定的种类: 0: category; 1: location; 2: vehicle_type; 3: mode_type 0: category; 1: location; 2: vehicle_type; 3: mode_type 0: category; 1: location; 2: vehicle_type; 3: mode_type . 0: category; 1: location; 2: vehicle_type; 3: mode_type So here comes the good part: just change your json to look something like this: 好地方就在这里:只需将json更改为如下所示:

Section:
[
  {
     title: "category",
     value: {}
  },
  {
     title: "location",
     value: {}
  },
  {
     title: "vehicle_type",
     value: {}
  },
  {
     title: "mode_type",
     value: {}
  }
]

Having this json, you just go through the Section ordered elements, check the title of the element, and create the corresponding Object. 有了这个json,您只需遍历Section有序的元素,检查元素的标题,并创建相应的Object。 This way you can achieve what you are after. 这样,您就可以实现所追求的目标。

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

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