2013-07-11 64 views
-2

如何對此json進行排序。下面的json是在瀏覽器中輸出的。如何對此JSON進行排序

{ 
event: { 
July: [ 
{ 
createdSeatMap: "yes", 
status: "1", 
completed: "1", 
eventPayment: "0", 
} 
], 
Aug: [ 
{ 
createdSeatMap: "yes", 
status: "1", 
completed: "1", 
eventPayment: "0", 
} 
], 
Sept: [ ], 
Oct: [ ], 
Nov: [ ], 
Dec: [ 
{ 
createdSeatMap: "yes", 
status: "1", 
completed: "1", 
eventPayment: "0", 
stamp: "1386019800" 
}, 
{ 
createdSeatMap: "yes", 
status: "1", 
completed: "1", 
eventPayment: "0", 
stamp: "1387921620" 
} 
], 
Jan: [ ], 
Feb: [ ], 
Mar: [ ], 
Apr: [ ], 
May: [ ], 
June: [ ] 
} 
} 

下面是在取出時的控制檯。

Apr =  (
); 
Aug =  (
      { 

     createdSeatMap: "yes", 
     status: "1", 
     completed: "1", 
     eventPayment: "0", 
    } 
); 
Dec =  (
      { 
createdSeatMap: "yes", 
status: "1", 
completed: "1", 
eventPayment: "0", 
stamp: "1386019800" 
}, 
{ 
createdSeatMap: "yes", 
status: "1", 
completed: "1", 
eventPayment: "0", 
stamp: "1387921620" 
} 
); 
Feb =  (
); 
Jan =  (
); 
July =  (
      { 
     createdSeatMap: "yes", 
status: "1", 
completed: "1", 
eventPayment: "0", 
    } 
); 
June =  (
); 
Mar =  (
); 
May =  (
); 
Nov =  (
); 
Oct =  (
); 
Sept =  (
); 

下面是代碼我使用

NSMutableArray *dictAllValues = [[NSMutableArray alloc]init]; 
NSMutableDictionary *getDataDict = [[NSMutableDictionary alloc]init]; 

getDataDict = [self.mGetDataDict valueForKey:@"event"]; 

NSArray *dictAllKeys = [[NSArray alloc] init]; 

dictAllKeys = [getDataDict allKeys]; 

NSLog(@"dict :%d", [dictAllKeys count]); 
for(int i=0; i<[getDataDict count]; i++) 
{ 
    NSString *strKey = [dictAllKeys objectAtIndex:i]; 
    [dictAllValues addObjectsFromArray:[getDataDict valueForKey:strKey]]; 
    if(i==0) 
    { 

     NSArray *cVals = [dictAllValues valueForKey:strKey]; 
     int c = [cVals count]; 
     NSLog(@"values:%@", cVals); 
     NSLog(@"values:%@", dictAllValues); 
     [self.mArrMOneEvents addObject:[NSString stringWithFormat:@"%d", c]]; 
     self.mArrMOneEtType = [dictAllValues valueForKey:@"type"]; 
     self.mArrMOneEtTime = [dictAllValues valueForKey:@"time"]; 
     self.mArrMOneEtSeatMap = [dictAllValues valueForKey:@"createdSeatMap"]; 
     self.mArrMOneEtSeatId = [dictAllValues valueForKey:@"seatMapId"]; 
     self.mArrMOneEtQuant = [dictAllValues valueForKey:@"quantity"]; 
     self.mArrMOneEvents = [dictAllValues valueForKey:@"eventName"]; 
     self.mArrMOneETLoc = [dictAllValues valueForKey:@"location"]; 
     self.mArrMOneEtEId = [dictAllValues valueForKey:@"id"]; 
     self.mArrMOneEtDesc = [dictAllValues valueForKey:@"description"]; 
     self.mArrMOneEtDate = [dictAllValues valueForKey:@"date"]; 
     self.mArrMOneEtCurr = [dictAllValues valueForKey:@"currency"]; 
     self.mArrMOneEtCost = [dictAllValues valueForKey:@"cost"]; 
     self.mArrMOneEtCateg = [dictAllValues valueForKey:@"category"]; 
     self.mArrmOneEtImg = [dictAllValues valueForKey:@"banner"]; 
     NSLog(@"array one:%d", [self.mArrMOneEvents count]); 
    } 
} 

+(NSString *)sendRequest:(NSMutableURLRequest *)request 
{ 
NSHTTPURLResponse *response; 
NSError *error; 
NSData *responseData; 
responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
if(responseData && [responseData length]) { 
    NSString* aStr; 
    aStr = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding]; 
    return aStr; 
} 
else { 
    return @"No Record Found"; 
}  
} 

+(NSMutableDictionary *)forgotPassw:(NSDictionary *)mailId 
{ 
NSString *urlStr = [NSString stringWithFormat:@"http://myvnt.co/api2/forgotpassword/%@",[mailId valueForKey:@"mailId"]]; 

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: 
           [NSURL URLWithString: 
           [urlStr stringByAddingPercentEscapesUsingEncoding: 
            NSUTF8StringEncoding]]]; 

NSString *responseString = [MtApi sendRequest:request]; 
NSLog(@"response:%@", responseString); 
NSError *error; 
NSData *jsonData = [responseString dataUsingEncoding:NSUTF16StringEncoding]; 
NSDictionary *results = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error]; 

NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithDictionary:results]; 
NSLog(@"dict in API-------------%@",dict); 

return dict; 
} 

如何解決這請大家指導以上。提前致謝。

+0

我發現了一個問題previsouly張貼,希望這可以幫助你。 [json數組和排序](http://stackoverflow.com/questions/14601229/json-arrays-and-sorting) – Keeano

回答

1

JSON本質上是一個NSDictionary,它不是一個有序集合。爲了有一個一致的排序,你需要手動打印:

NSArray sortedKeys = [json keysSortedByValueUsingSelector:@selector(someSelectorWhichSortsByMonthOrder)]; 

[sortedKeys enumerateObjectsUsingBlock:^(NSString *month, NSUInteger idx, BOOL *stop){ 
    NSLog(@"%@ = %@", month, json[month]); 
}]; 

不過話說回來,順序心不是改變字典的內容,那麼有沒有你真的關心它看起來如何打印在控制檯上的任何理由?

編輯:既然你想爲一個表視圖的排序,這是我會做什麼:

不與上面的排序打擾,只是定義它包含月份名稱(按順序)一個數組。在的cellForRowAtIndexPath:使用indexPath.row獲取當前月份,然後你可以通過調用訪問一個月的數據:

NSString *cellMonth = self.orderedMonthNames[indexPath.row]; 
NSDictionary *monthData = self.jsonDictionary[cellMonth]; 
+0

這是說沒有選擇器可用someSelectorWhichSortsByMonthOrder。我已經發布我的代碼也請檢查。 –

+0

顯然你需要決定你在那裏使用哪個選擇器,我不知道哪個選擇器會根據月份進行排序,你可能需要編寫一個方法 – wattson12

+0

是的,我需要關心訂單,因爲我在表中顯示幾個月它開始從當前月份開始,所以值需要在各自的月份中顯示。 –