0

我正在尝试获取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"
      }
    }
  }
}

这样做的问题是,在这个数量的级别中,行和内部也是动态的。在这我需要获取所有行的开/关值,水平虎钳。我怎样才能做到这一点?

这是我到目前为止所尝试的:

for (NSString *key in [self.mGetDataDict allKeys]) {
    NSLog(@"Accessing .... %@", key);
    level = key;
    for (NSString *rowKey in self.mGetDataDict[key]) {
        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];
            }
        }
        NSLog(@"--------ROW OVER------------%@\n", dictRow);
        [arrSeats addObject:[dictRow valueForKey:@"total"]];
        [arrType addObject:[dictRow valueForKey:@"type"]];
        NSLog(@"--------seats in row------------%@\n", arrSeats);

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

2 回答 2

2

What you will need to do is to get that response into an object (in your case an NSDictionary) and then enumerate through the keys to parse it out. This is easy to do:

You will probably want to use the NSDictionary method - enumerateKeysAndObjectsWithOptions:usingBlock:

Here is the class reference: http://developer.apple.com/library/ios/#documentation/cocoa/reference/foundation/Classes/NSDictionary_Class/Reference/Reference.html

于 2013-05-30T12:30:44.737 回答
1

尝试这个,

var json_data = {your_data_json};
for (var level_index in json_data){
//level_index - level values like level1, level3
//json_data[level_index] - to get value object, i mean row1{}
    for (var row_index in json_data[level_index]){
    //row_index as row1 or row10, ....
        for (var record_index in json_data[level_index][row_index]){
            if (record_index == "attr"){
            //you r in attr {} object
            }
            else { 
            //getting
            // record_index 1,2,3,....
            //json_data[level_index][row_index][record_info] - on, on, on,.........
            }
        }
    }
}
于 2013-05-30T12:48:51.870 回答