2012-05-29 53 views
1

可能重複:
JSONkit sorting issues爲什麼json值按字母順序排序iOS?

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

字典沒有一個訂單。 –

+0

我使用這個json在分組的UITableView中顯示數據,我需要它來自數據庫的順序,我該如何處理我所需要的? – Angie

+0

Thanxs的鏈接。 – Angie

回答

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 
    } 
} 
+0

是的,我可以改變JSON格式。我將如何閱讀它?我在我的問題中更新了我想用數據顯示的表格表格。 – Angie

+0

我改變了JSON格式並讀取了上面提到的數據,它工作得非常好。感謝您的幫助。 – Angie

0

字典沒有其項目訂購的所有數據。排序只能通過iOS完成,以便很好地顯示。但是,您絕對不應該假定字典中的任何項目的排序。

1

A 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訂購集裝箱。這就是傑弗裏托馬斯陳述的原因,你應該爲每個JSON條目使用數組。

但你也可以嘗試使用像水木清華:

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

這也看起來像它會工作,也感謝你的幫助。 – Angie