首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么json值按字母顺序iOS排序?

为什么json值按字母顺序iOS排序?
EN

Stack Overflow用户
提问于 2012-05-29 13:36:27
回答 3查看 1.5K关注 0票数 1

可能重复:

JSONkit sorting issues

我有下面的jSON

代码语言:javascript
复制
[
  {
    "Pending": 67,
    "Past Due": 63,
    "Invites": 0
  },
  {
    "Reading Approval": 4,
    "Schedule Approval": 16,
    "Session Assignment": 1
  },
  {
    "Reading Approval": 3,
    "Schedule Approval": 20,
    "Class Approval": 20,
    "Module Approval": 2,
    "Training Plan Review": 2,
    "Job Confirmation": 1
  }

]

当我将json字符串分配给nsarray时,字典中的值会按字母顺序排序。

代码语言:javascript
复制
NSArray *arr = [strResult JSONValue]; 

如何保持与json字符串相同的顺序?

代码语言:javascript
复制
arr = 
{
  Invites = 0;
  "Past Due" = 63;
  Pending = 67;
},
{
  "Reading Approval" = 4;
  "Schedule Approval" = 16;
  "Session Assignment" = 1;
},
{
  "Class Approval" = 20;
  "Job Confirmation" = 1;
  "Module Approval" = 2;
  "Reading Approval" = 3;
  "Schedule Approval" = 20;
  "Training Plan Review" = 2;
}
)

这是我想显示的表:

代码语言:javascript
复制
Group 1
  Pending    67
  Past Due   63
  Invites    0

Group 2
  Reading Approval    4
  Schedule Approval   16
  Session Assignment  1

Group 3
  Reading Approval      3
  Schedule Approval     20
  Class Approval        20
  Module Approval       2
  Training Plan Review  2
  Job Confirmation      1
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-05-29 14:33:07

使用标准的JSON解析器,您将需要更改数据。

代码语言:javascript
复制
[
  [
    { "Pending": 67 },
    { "Past Due": 63 },
    { "Invites": 0 }
  ],
  [
    { "Reading Approval": 4 },
    { "Schedule Approval": 16 },
    { "Session Assignment": 1 }
  ],
  [
    { "Reading Approval": 3 },
    { "Schedule Approval": 20 },
    { "Class Approval": 20 },
    { "Module Approval": 2 },
    { "Training Plan Review": 2 },
    { "Job Confirmation": 1 }
  ]
]

假设您可以更改JSON格式。

如何访问数据。假设您有一个节数组,并且知道节和行索引。

代码语言:javascript
复制
NSArray *sections = ...
NSUInteger sectionIndex = ...
NSUInteger rowIndex = ...

NSArray *rows = [sections objectAtIndex:sectionIndex];
NSDictionary *cell = [rows objectAtIndex:rowIndex];

NSString *name = [[cell allKeys] objectAtIndex:0];
NSNumber *value = [cell objectForKey:name];

// What ever you need to do

如果您想迭代所有的数据

代码语言:javascript
复制
NSArray *sections = ...

for (NSArray *rows in sections) {
    for (NSDictionary *cell in rows) {
        NSString *name = [[cell allKeys] objectAtIndex:0];
        NSNumber *value = [cell objectForKey:name];

        // What ever you need to do
    }
}
票数 2
EN

Stack Overflow用户

发布于 2012-05-29 15:21:43

NSDictionary无序容器对象。如果它被打印出来,它将按字母顺序排序。但是没有保证的秩序。

description:的文档显示:

代码语言:javascript
复制
If each key in the dictionary is an NSString object, the entries are listed in ascending order by key, otherwise the order in which the entries are listed is undefined. [...]

allKeys:说:

代码语言:javascript
复制
The order of the elements in the array is not defined.

allValues还说:

代码语言:javascript
复制
The order of the elements in the array is not defined.

但是NSArray是一个命令的容器。这就是Jeffery Thomas statet的原因,您应该为每个JSON条目使用数组。

但你也可以试着用这样的气味:

代码语言:javascript
复制
[
  [
    "keys": {"Pending","Past Due","Invites"},
    "values": {67,63,0}
  ],
  [
    "keys": {"Reading Approval","Schedule Approval","Session Assignment"},
    "values": {4,16,1},
  ],
  [
    "keys": {"Reading Approval","Schedule Approval","Class Approval",...},
    "values": {3,20,20,...},
  ]
]
票数 1
EN

Stack Overflow用户

发布于 2012-05-29 15:14:09

字典中的项目没有排序。排序是由iOS完成的,只是为了很好地显示它。但是,您不应该假设字典中的项目排序。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10800285

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档