从字典数组中获取字典值的范围

get the range of dictionary values from Array of dictionary

在我的回复中 JSON 我需要从字典数组中解析一系列值。 这是我的回复,

“List”: [
      {
         "event_desc": "On"
      },
      {
         "event_desc": “Go”
      },
      {
         "event_desc": “Start”
      },
      {
         "event_desc": "Off”
      },
      {
         "event_desc": “Rework”
      },
      {
         "event_desc": "On"
      },
      {
         "event_desc": “Start”
      },
      {
         "event_desc": "Off”
      },
      {
         "event_desc": “Swipe”
      },
      {
         "event_desc": "On"
      },
      {
         "event_desc": “Work”
      },
      {
         "event_desc": “Repair”
      },
      {
         "event_desc": “Off”
      },
      {
         "event_desc": "On"
      }
]

这里我想按 "On" 和 "Off" 的范围分隔值 即

list1=[ {event_desc:On},{event_desc:Go},{event_desc:Start},{event_desc:Off}]
list2=[{event_desc:On},{event_desc:Start},{event_desc:Off}]
list3= [{event_desc:On},{event_desc:Work},{event_desc:Repair},{event_desc:Off}]

帮帮我,先谢谢了

你可以这样做,

NSArray *yourArr;   // posted in question
NSMutableArray *resultArr = [[NSMutableArray alloc]init];
NSMutableArray *tempArr = [[NSMutableArray alloc]init];
BOOL flag = NO;
for (int i = 0; i < yourArr.count; i++) {
    NSDictionary *dic = [yourArr objectAtIndex:i];
    if ([[dic objectForKey:@"event_desc"] isEqualToString:@"On"]) {
        flag = YES;
    }
    if (flag) {
        [tempArr addObject:dic];
    }
    if ([[dic objectForKey:@"event_desc"] isEqualToString:@"Off"]) {
        flag = NO;
        [resultArr addObject:[tempArr mutableCopy]];
        [tempArr removeAllObjects];
    }
}
NSLog(@"your result array : %@",resultArr);
    NSArray *parsedJson = nil; //Should have your parsed json list array here
    NSMutableArray *eventList = [@[] mutableCopy]; // This will be your final array of list events
    NSMutableArray *eventEntry = [@[] mutableCopy]; // Will contain each list event from event_desc On to event_desc Off

    for (NSDictionary *event in parsedJson) {

        if ([event[@"event_desc"] isEqualToString:@"On"]) {
            [eventEntry removeAllObjects];
            [eventList addObject:eventEntry];

        }else if(([event[@"event_desc"] isEqualToString:@"Off"]) ){
            [eventEntry addObject:event];
            [eventList addObject:eventEntry]; // Add each eventEntry to main array as its complete.

        }else{
            [eventEntry addObject:event];

        }
    }

    NSLog(@"Result eventList array : %@",eventList);