-3

尝试获取下面的 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,然后以某种顺序更进一步,但不是如上所示的顺序。希望我说清楚了。请指导以上。提前致谢。

4

2 回答 2

1

例如,更换

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

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

(尽管尝试为“rowDict”想一个更具描述性的名称)

是的,它有更多的字符,但在后面的几行中,您可以参考rowDict[something],也许以后还要参考,valueDict[something]而不是长索引链。更有效,更容易保持直线。

于 2013-06-03T15:48:34.303 回答
0

我完全同意Hot Licks,遵循你的代码和你的问题的想法真的很复杂 :) 据我了解,你在问,为什么你没有按照它们在 json 中的方式对密钥进行排序。

您用来获取它们的工具是将您的 json 转换为 NSDictionary,而NSDictionary不保证键以任何方式排序或按原样保留。因此,您正在使用比较器 - @selector(caseInsensitiveCompare:)以某种方式对键进行排序。

所以答案是:如果您想以复杂的方式对键进行排序(“将顺序保留在 json 中”不能自行完成,必须在从 NSDictionary 检索键时这样做),您必须实现您的自己的比较器。那不是caseInsensitiveCompare:您必须使用您的自定义选择器。您可以在 stackoverflow 周围找到大量示例

希望这可以帮助。

于 2013-06-03T15:55:42.027 回答