如何对这个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]);
}
}
这是我试过的
NSArray *keys = [[getDataDict allKeys] sortedArrayUsingSelector: @selector(compare:)];
NSMutableArray *array = [NSMutableArray arrayWithCapacity: [keys count]];
for (NSString *key in keys) {
[array addObject: [getDataDict objectForKey: key]];
}
NSLog(@"array:%@", array);
下面是描述此要求的图像。 需要与来自服务器的方式相同的响应,请在上面指导。提前谢谢。
答案 0 :(得分:3)
排序字典键的复杂方法
NSCalendar *calender = [NSCalendar currentCalendar];
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"MMM"];
NSDateComponents *components = [NSDateComponents new];
components.month = 1;
NSMutableArray *months = [@[] mutableCopy];
NSDate *date = [NSDate date];
[months addObject:[dateFormatter stringFromDate:date]];
while ([months count]<12){
date = [calender dateByAddingComponents:components toDate:date options:0];
[months addObject:[dateFormatter stringFromDate:date]];
}
//A sample dictionary to test
NSDictionary *dictionary = @{@"Jan": @"",
@"Feb": @"",
@"Mar": @"",
@"Apr": @"",
@"May": @"",
@"Jun": @"",
@"Jul": @"",
@"Aug": @"",
@"Sep": @"",
@"Oct": @"",
@"Nov": @"",
@"Dec": @""};
NSArray *keys = [[dictionary allKeys] sortedArrayUsingComparator:
^NSComparisonResult(NSString * key1, NSString * key2) {
NSUInteger index1 = [months indexOfObject:[key1 substringToIndex:3]];
NSUInteger index2 = [months indexOfObject:[key2 substringToIndex:3]];
return [@(index1) compare:@(index2)];
}];
NSLog(@"keys : %@",keys);
答案 1 :(得分:1)
您遇到的主要问题是您的JSON响应未正确格式化。您的JSON响应中有许多不需要的东西。 看看你的JSON响应:
{
event: {
July: [
{
createdSeatMap: "yes",
status: "1",
completed: "1",
eventPayment: "0",
}
]
}
}
在您的情况下,event
应该是不同月份的事件数组。现在,在每个数组中,您将拥有该月的所有事件。将键(在上面的示例中,“July”)作为值添加到数组,而不是将其作为键。请参阅响应的示例:
{
event: {
[
Month : "July", <<--- //See this Line.
{
createdSeatMap: "yes",
status: "1",
completed: "1",
eventPayment: "0",
}
]
}
}
当您的服务器返回字典时,您的响应将永远不会维护订单。您应该从服务器端修改您的响应,如上所述。