-2

如何排序这个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;
}

如何排序这个请指导上面。提前致谢。

4

1 回答 1

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];
于 2013-07-11T07:44:19.963 回答