【问题标题】:Why does json values get sorted alphabetically iOS? [duplicate]为什么 json 值按字母顺序排序 iOS? [复制]
【发布时间】:2012-05-29 13:36:27
【问题描述】:

可能重复:
JSONkit sorting issues

我有以下 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

【问题讨论】:

  • 字典没有订单。
  • 我正在使用这个 json 在分组的 UITableView 中显示数据,并且我需要它按照来自数据库的顺序排列,我怎样才能接近我需要的?

标签: ios json nsarray nsdictionary


【解决方案1】:

使用标准 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
    }
}

【讨论】:

  • 是的,我可以更改 JSON 格式。我将如何阅读它?我在我的问题中更新了我想用数据显示的表格。
  • 我更改了 JSON 格式并读取了您上面提到的数据,效果非常好。感谢您的帮助。
【解决方案2】:

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

[
  [
    "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,...},
  ]
]

【讨论】:

  • 这看起来也可行,也感谢您的帮助。
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-23
    • 2013-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多