2013-04-11 67 views
2

我有一個從請求返回的JSON數組。
數據作爲這樣的(它來自外部源,並且不能從服務器改變):在JSON中NSDictionary/NSArray中正確保存KEYS

[{"clients": 
{"name":"Client name here","telephone":"00000","email":"[email protected]","website":"www.clientname.com"} 
}] 

我接收JSON數組的順序。

我有以下的Objective-C:

//jsonString is the json string inside type NSString. 
NSMutableDictionary *tempDict = [jsonString JSONValue]; 
NSMutableDictionary *clients = [tempDict objectForKey:@"clients"]; 

for(NSString *key in clients) { 
     id value = [tempDict objectForKey:key]; 
      for(NSString *itemKey in key) { 
       NSLog(@"ITEM KEY %@: ",itemKey); 

      } 
} 

它打印按鍵的順序是:
電話
電子郵件

網站

我需要它來打印如何它被接收(例如):
名稱
電話
電子郵件
網站

我讀過字典根據定義未排序,所以很高興使用數組而不是字典。

但我已經(使用keysSortedByValueUsingSelector)看到在計算器上就可能解決多個線程:
Getting NSDictionary keys sorted by their respective values
Can i sort the NSDictionary on basis of key in Objective-C?
sort NSDictionary keys by dictionary value into an NSArray
Can i sort the NSDictionary on basis of key in Objective-C?

我曾嘗試他們和我越來越行不通。不知道這是否只是我的實現是錯誤的。

我試圖(嘗試1):

NSArray *orderedKeys = [clients keySortedByValueUsingComparator:^NSComparisonResult(id obj1, id obj2){ 
    return [obj1 compare:obj2]; 
}]; 

for(NSString *key in orderedKeys) { 
     id value = [tempDict objectForKey:key]; 
      for(NSString *keyThree in key) { 
       NSLog(@"API-KEY-ORDER %@: ",keyThree); 

      } 
} 

接收錯誤:[__NSArrayM keySortedByValueUsingComparator:]: unrecognized selector sent to instance

還試圖(嘗試2):

NSArray *sortedKeys = [[clients allKeys] sortedArrayUsingSelector: @selector(compare:)]; 
NSMutableArray *sortedValues = [NSMutableArray array]; 
for (NSString *key in sortedKeys){ 
    [sortedValues addObject: [tempDict objectForKey: key]]; 
    //[sortedValues addObject: [all objectForKey: key]]; //failed 
} 

但在選擇另一個錯誤,即使客戶是一個的NSMutableDictionary。

[__NSArrayM allKeys]: unrecognized selector sent to instance 0x4b6beb0 

我在哪裏,我認爲我會遍歷NSMutableDictionary,並手動將它們放入一個新NSMutableArray以正確的順序點。

任何想法將非常感激?
OR anybodies code snippet attempt at my problem will equal to appreciate。

+0

看起來好像'clients'是一個'NSArray',你試圖在它上面執行'NSDictionary'的選擇器 – 2013-04-11 15:42:34

+0

是的,我不在乎它爲什麼這麼想。你可以在上面看到'clients'是一個'NSMutableDictionary'。 – 2013-04-11 15:44:12

+0

在客戶端啓動後放置一個斷點,並在控制檯中輸入「po clients」。控制檯顯示什麼類? – 2013-04-11 15:48:23

回答

1

首先,您看到的異常是因爲選擇器名稱不正確。你應該使用keysSortedByValueUsingComparator。 其次,你不能對鍵進行排序,因爲你從服務器獲得它們的順序似乎沒有任何特定的順序。如果你必須按順序安裝它們,然後用一個標籤(「索引」)從服務器發送它們,然後用這個標籤進行排序。

+0

我以爲我使用上面的'keySortedByValueUsingComparator'?我擔心我無法修改它們從服務器到達的方式。任何代碼片段的修復都會很有用。 – 2013-04-11 15:36:09

+1

你錯過了's'。這是鑰匙,不是鑰匙。 – 2013-04-11 15:39:34

+0

也許如果你能解釋爲什麼你必須保持訂單,我可以幫你找到解決方案。 – 2013-04-11 15:41:31

2

不僅NSDictionary原則上是無序的,而且JSON對象也是如此。這些JSON對象是相同

[{"clients": 
{"name":"Client name here","telephone":"00000","email":"[email protected]","website":"www.clientname.com"} 
}] 

[{"clients": 
{"website":"www.clientname.com","name":"Client name here","telephone":"00000","email":"[email protected]"} 
}] 

如果服務器想要訂購鍵,它需要發送一個陣列。當然,您可以使用keysSortedByValueUsingComparator:方法以按排序順序獲取密鑰。