2013-06-03 97 views
-3

試圖獲取下面的json,它的所有標記都是動態的,而不是固定的。與JSON相比,以相反的順序獲取數據

{ 
Level1: { 
row1: { 
1: "on", 
2: "off", 
3: "off", 
4: "on", 
5: "on", 
6: "on", 
7: "on", 
8: "on", 
9: "on", 
10: "on", 
11: "on", 
12: "on", 
13: "on", 
14: "on", 
15: "on", 
16: "on", 
17: "on", 
18: "on", 
19: "on", 
20: "on", 
attr: { 
total: "20", 
type: "Gold" 
} 
}, 
row10: { 
1: "on", 
2: "on", 
3: "on", 
4: "on", 
5: "on", 
6: "on", 
7: "on", 
8: "on", 
9: "on", 
10: "on", 
11: "on", 
12: "on", 
13: "on", 
14: "on", 
15: "on", 
16: "on", 
17: "on", 
18: "on", 
19: "on", 
20: "on", 
attr: { 
total: "20", 
type: "Bronze" 
} 
} 
}, 
Level3: { 
row1: { 
1: "on", 
2: "on", 
3: "on", 
4: "on", 
5: "on", 
6: "on", 
7: "on", 
8: "on", 
9: "on", 
10: "on", 
11: "on", 
12: "on", 
13: "on", 
14: "on", 
15: "on", 
16: "on", 
17: "on", 
18: "on", 
19: "on", 
20: "on", 
attr: { 
total: "20", 
type: "Gold" 
} 
}, 
row5: { 
1: "on", 
2: "on", 
3: "on", 
4: "on", 
5: "on", 
6: "on", 
7: "on", 
8: "on", 
9: "on", 
10: "on", 
11: "on", 
12: "on", 
13: "on", 
14: "on", 
15: "on", 
16: "on", 
17: "on", 
18: "on", 
19: "on", 
20: "on", 
attr: { 
total: "20", 
type: "Bronze" 
} 
} 
} 
} 

這是我到目前爲止所嘗試的。

NSString *level; 
for(NSString *key in [[self.mGetDataDict allKeys]sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]) 
{ 
    NSLog(@"Accessing .... %@",key); 
    level = key; 

    for(NSString *rowKey in self.mGetDataDict[key]) 
    { 
     [arrayLevels addObject:level]; 

     NSLog(@"%@",rowKey); 
     [arrRows addObject:rowKey]; 
     for(NSString *valueKey in self.mGetDataDict[key][rowKey]) 
     { 
      // NSLog(@"%@ -> %@",valueKey,self.mGetDataDict[key][rowKey][valueKey]); 
      if([valueKey isEqualToString:@"attr"]) 
      { 
       dictRow = self.mGetDataDict[key][rowKey][valueKey]; 
      } 
      else{ 
       //dictRow = self.mGetDataDict[key][rowKey][valueKey]; 
       [arrSeatsStatus addObject:self.mGetDataDict[key][rowKey][valueKey]]; 
      } 
     } 
     NSLog(@"--------ROW OVER------------%@\n", dictRow); 
     [arrSeats addObject:[dictRow valueForKey:@"total"]]; 
     NSString *strTypes = [NSString stringWithFormat:@"%@ %@", [arrayLevels objectAtIndex:i], [dictRow valueForKey:@"type"]]; 
     [arrTypes addObject:strTypes]; 

      NSMutableArray *array = [NSMutableArray array]; 
      array = [arrSeatsStatus copy]; 
      [seatsDict setObject:array forKey:[NSString stringWithFormat:@"%d",i]]; 
      i++; 
      [arrSeatsStatus removeAllObjects]; 

     NSLog(@"--------seats dict------------%@\n", seatsDict); 

     NSLog(@"--------seats in row------------%@\n", arrSeats); 
     // NSLog(@"--------seats status------------%@\n", arrStatus); 
     NSString *row = [NSString stringWithFormat:@"%@",[arrSeats objectAtIndex:0]]; 
     rows = [row integerValue]; 
    } 
    NSString *num = [arrSeats valueForKeyPath: @"@sum.self"]; 
    tot = [num integerValue]; 
    [arrTot addObject:[NSString stringWithFormat:@"%d",tot]]; 
    tot = 0; 
    //[arrSeats removeAllObjects]; 
    NSLog(@"--------ROW OVER tot seats------------%@\n", arrTot); 
    NSLog(@"--------------------seats:%@\n", num); 
    NSLog(@"--------------------\n"); 
} 

問題是,當數據提取時,它不是來自JSON。例如,當上面的代碼運行時,它首先獲取level2,然後row2以某種順序進一步進入,但不是如上所示的順序進行。希望我說清楚。請指導以上。提前致謝。

+1

只是一個提示:當你深入鑽研時,不要在你的主數據結構的引用中加入'[]'值。相反,將NSDictionary指針保存到組成對象。這將簡化一些事情。但是,代碼是複雜的方式,否則太複雜了。您需要查看其他方法來簡化它。 –

+0

@熱舔請提供一個例子,如果你能更容易理解。 - –

+0

請更新您的問題,提供更多關於您試圖達到的結果的詳細信息。你的問題很不清楚。 – rmaddy

回答

1

例如,更換

for(NSString *rowKey in self.mGetDataDict[key]) 

NSDictionary *rowDict = self.mGetDataDict[key]; 
for (NSString *rowKey in rowDict) 

(雖然儘量想一個更具描述性的名稱爲 「rowDict」)

是的,它更多的字符,但在以後的你可以參考rowDict[something]行,也許以後仍然valueDict[something]而不是長索引鏈。更高效和更容易保持直線。

+0

非常感謝你,讓我走上正軌。 –

0

我完全同意熱舔,它是真的很複雜,遵循你的代碼和你的問題的想法:)據我所知,你問,爲什麼你沒有你的鑰匙排序方式在json中。

你用來獲取它們的工具是將你的json轉換成NSDictionary,而NSDictionary不保證這些鍵以任何方式被排序,或者保留在它們被放置的位置。因此,您正在使用比較器 - @selector(caseInsensitiveCompare :)以某種方式對鍵進行排序。

所以答案應該是: 如果你想的鑰匙,複雜的方式(「離開的命令,因爲它是在JSON」不能由自己完成排序,必須做到從檢索鍵時NSDictionary),你必須實現你自己的比較器。這是代替caseInsensitiveCompare:你必須使用你的自定義選擇器。你可以找到的例子in great amount around stackoverflow

希望這會有所幫助。

+0

應該指出的是,JSON並沒有將鍵定義爲任何順序。沒有像「JSON中的訂單」那樣的東西。 –

+0

嗯,是的。只是想用文字來表達作者希望在提供的片段中保留鍵值命令的可能性*。 – makaron