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

标签 ios json nsarray nsdictionary

Possible Duplicate:
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解析器,您将需要更改数据。

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

关于ios - 为什么json值按iOS字母顺序排序? ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10800285/

相关文章:

ios - 进入前台时出现 UIRefreshControl 错误

android - GitHub API : finding number of stars, 提交和发布

c# - 无法从 NULL 读取属性 "Patient"

ios - 如何快速将 NSArray 合并到 NSMutableArray?

iphone - NSArray 排序和隔离

ios - iOS 7.1 中的 UILabel 虚线颜色错误

ios - 在 iphone X 的安全区域设置 Webview

ios - 用于在 iOS 中解析的 JSON 和 XML 包装器

PHP JSON 数据到 Xcode Swift 数组

ios - 为什么我的数组返回 null?