1

可能重复:
JSONkit 排序问题

我有以下json

[
  {
    "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 时,字典中的值会按字母顺序排序。

NSArray *arr = [strResult JSONValue]; 

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

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;
}
)

这是我要显示的表格:

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
4

3 回答 3

2

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

[
  [
    { "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 *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

如果要遍历所有数据

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
    }
}
于 2012-05-29T14:33:07.380 回答
1

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

文档description:说:

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:说:

The order of the elements in the array is not defined.

并且allValues还说:

The order of the elements in the array is not defined.

但是NSArray是一个有序的容器。这就是 Jeffery Thomas 声明您应该为每个 JSON 条目使用数组的原因。

但是您也可以尝试使用 smth ,例如:

[
  [
    "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,...},
  ]
]
于 2012-05-29T15:21:43.427 回答
0

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

于 2012-05-29T15:14:09.430 回答