-3

Trying to fetch the json below, it's all tags are dynamic not fixed.

{
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"
}
}
}
}

This is what i tried so far.

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");
}

The problem is that when data fetched it is not in order as coming from JSON. For example when above code runs it first fetch level2 then row2 then further in some order but not in sequence as shown above. Hope i made it clear. please guide for above. Thanks in advance.

iPhone Programmatically
  • 1,211
  • 2
  • 22
  • 54
  • 1
    Just a hint: As you drill deeper, don't keep adding `[]` values to the reference to your main data structure. Instead, save the NSDictionary pointers to the constituent objects. That will simplify things a bit. But the code is way overcomplicated otherwise -- too complicated to follow. You need to look at other ways to simplify it. – Hot Licks Jun 03 '13 at 11:51
  • @Hot Licks please provide an example if you can it will be easier to understand. – – iPhone Programmatically Jun 03 '13 at 12:37
  • Please update your question with more details on what results you are trying to achieve. Your question is very unclear. – rmaddy Jun 03 '13 at 17:18

2 Answers2

1

Eg, replace

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

with

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

(though try to think of a more descriptive name for "rowDict")

Yes, it's more characters, but in later lines you can refer to rowDict[something], and maybe later still valueDict[something] rather than the long index chains. Both more efficient and easier to keep straight.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
0

I totally agree with Hot Licks, it's really complicated to follow your code and your question's idea :) As far as I understand, you're asking, why you don't have your keys sorted in the way they are in the json.

The tool you're using to fetch them is converting your json into NSDictionary, whereas NSDictionary doesn't guarantee the keys are sorted in any way or left as they were put in it. Therefore you're using a comparator - @selector(caseInsensitiveCompare:) to sort the keys somehow.

So the answer would be: If you want to sort the keys in sophisticated way (to "leave the order as it's in json" can't be done by itself, you must do that when retrieving keys from NSDictionary), you must implement your own comparator. That is instead of caseInsensitiveCompare: you must use your custom selector. The examples you can find in great amount around stackoverflow

Hope this helps.

Community
  • 1
  • 1
makaron
  • 1,585
  • 2
  • 16
  • 30
  • It should be noted that JSON does not define the keys to be in any order. There is no such thing as "the order as it's in JSON". – Hot Licks Jun 03 '13 at 16:34
  • Well, yes. Just was trying to express with the words the author's wish to leave the key-value order as it is *visualized* in the snippet provided. – makaron Jun 03 '13 at 16:57